<C#任务导引教程>练习七

//55,类的声明示例
using System;
class Date
{
    public int year;
    public int month;
    public int day;
    public void print()
    {
        Console.WriteLine("{0}/{1:D2}/{2:D2}", year, month, day);
    }
}
class Program
{
    static void Main()
    {
        Date t = new Date();
        Console.Write("请输入年:");
        t.year = Convert.ToInt32(Console.ReadLine());
        Console.Write("请输入月:");
        t.month = Convert.ToInt32(Console.ReadLine());
        Console.Write("请输入年:");
        t.day = Convert.ToInt32(Console.ReadLine());
        t.print();
    }
}
//56,利用“属性”访问私有数据成员示例
using System;
class Date
{
    int year;
    int month;//默认私有成员
    int day;
    public int Year
    {
        get { return year; }
        set { year = value; }
    }
    public int Month
    {
        get { return month; }
        set { month = value; }
    }
    public int Day
    {
        get { return day; }
        set { day = value; }
    }
    public string Print//定义日期信息属性
    {
        get
        {
            return Year + "/" + Month + "/" + Day;
        }
    }
}
class Program
{
    static void Main()
    {
        Date d = new Date();
        d.Year = 2010;
        d.Month = 4;
        d.Day = 20;
        Console.WriteLine(d.Print);
    }
}
//57,利用'属性"和"方法"访问私有数据成员示例
using System;
class Date
{
    int year, month, day;
    public int Year
    {
        set { year = value; }
    }
    public int Month
    {
        set { month = value; }
    }
    public int Day
    {
        set { day = value; }
    }
    public void Print()
    {
        Console.WriteLine("year:" + 2010);
        Console.WriteLine("month:" + 4);
        Console.WriteLine("day:" + 20);
    }
}
class Program
{
    public static void Main()
    {
        Date d = new Date();
        d.Print();
    }
}
//58,声明一个正方形类型和一个圆类型,然后分别求他们的面积
using System;
class Square
{
    int length;
    public void set(int l)
    {
        length = l;
    }
    public int area()
    {
        return length * length;
    }
}
class Circle
{
    int length;
    public void set(int l)
    {
        length = l;
    }
    public double area()
    {
        return 3.1416 * length * length / 4;
    }
}
class Program
{
    static void Main()
    {
        Square s = new Square();
        s.set(15);
        Console.WriteLine("正方形面积为:{0}", s.area());
        Circle c = new Circle();
        c.set(10);
        Console.WriteLine("圆形面积为:{0}", c.area());
        Console.ReadLine();
    }
}
//59,声明一个日期类,并进行日期的设置,判断闰年和输出等有关操作
using System;
class Date
{
    int year, month, day;
    public void set(int y, int m, int d)
    {
        year = y;
        month = m;
        day = d;
    }
    public bool isLeapYear()
    {
        return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    }
    public void print()
    {
        Console.WriteLine("{0}-{1:D2}-{2:D2}", year, month, day);
    }
}

class Program
{
    static void Main()
    {
        Date d = new Date();
        d.set(2000, 12, 6);
        if (d.isLeapYear())
            d.print();
    }
}
//60,构造函数的定义
using System;
class Box
{
    public Box(int h, int w, int l)
    {
        height = h;
        width = w;
        length = l;
    }
    public int volume()
    {
        return (height * width * length);
    }
    private int height, width, length;
}

class Program
{
    static void Main()
    {
        Box box1 = new Box(12, 25, 30);
        Console.WriteLine("长方形box1的体积是:{0}", box1.volume());
        Box box2 = new Box(15, 30, 21);
        Console.WriteLine("长方形box2的体积是:{0}", box2.volume());
    }
}
//61,使用不同参数用构造函数对成员变量赋初值示例。重载构造函数
using System;
class Box
{
    public Box()
    {
        height = 10;
        width = 10;
        length = 10;
    }
    public Box(int h)
    {
        height = h;
        width = 10;
        length = 10;
    }
    public Box(int h,int w)
    {
        height = h;
        width = w;
        length = 10;
    }
    public Box(int h,int w,int l)
    {
        height = h;
        width = w;
        length = l;
    }
    public int volume()
    {
        return (height * width * length);
    }
    private int height,width,length;
}
class Program
{
    static void Main()
    {
        Box box1=new Box();
        Console.WriteLine("长方形box1的体积是:{0}",box1.volume());
        Box box2=new Box(15);
        Console.WriteLine("长方形box2的体积是:{0}",box2.volume());
        Box box3=new Box(15,30);
        Console.WriteLine("长方形box3的体积是:{0}",box3.volume());
        Box box4=new Box(15,30,20);
        Console.WriteLine("长方形box4的体积是:{0}",box4.volume());
    }
}
//62,析构函数
using System;
class Date
{
    public Date(int y, int m, int d)
    {
        year = y;
        month = m;
        day = d;
        Console.WriteLine("{0}:构造函数已被调用.", day);
    }
    ~Date()
    {
        Console.WriteLine("{0}:析构函数已被调用", day);
    }
    public void print()
    {
        Console.WriteLine("{0}.{1}.{2}", year, month, day);
    }
    private int year;
    private int month;
    private int day;
}
class Program
{
    static void Main()
    {
        Date today = new Date(2010, 9, 1);
        Date tomorrow = new Date(2010, 9, 2);
        Console.Write("今天是:");
        today.print();
        Console.Write("明天是:");
        tomorrow.print( );
    }
}
//63,用不同对象调用同一个函数成员输出学生的平均分示例
using System;
class Student
{
    float score1;
    float score2;
    public Student(float s1, float s2)
    {
        score1 = s1;
        score2 = s2;
    }
    public float ave()
    {
        float average;
        average = (score1 + score2) / 2;
        return average;
    }
}
class Program
{
    static void Main()
    {
        Student Zhang = new Student(89.0f, 94.5f);
        Student Li = new Student(88.0f, 89.5f);
        Student Wang = new Student(90.0f, 82.5f);
        Console.WriteLine("张的平均成绩是:{0}", Zhang.ave( ));
        Console.WriteLine("李的平均成绩是:{0}", Li.ave());
        Console.WriteLine("王的平均成绩是:{0}", Wang.ave());
    }
}
//64,使用多个对象处理有关学生类问题的示例
using System;
class Student
{
    public Student(int n, int a, int s)
    {
         num=n;
         age = a;
         score = s;
    }
    public void print()
    {
        Console.WriteLine("num:{0}", num);
        Console.WriteLine("age:{0}", age);
        Console.WriteLine("score:{0}", score);
    }
    private int num, age, score;
}
class Program
{
    static void Main()
    {
        Student stud1 = new Student(1001, 18, 87);
        Student stud2 = new Student(1002, 19, 76);
        Student stud3 = new Student(1003, 18, 72);
        stud1.print();
        stud2.print();
        stud3.print();
    }
}
//65.使用对象数组来处理学生类问题
using System;
class Student
{
    public Student(int n, int a, int s)
    {
        num = n;
        age = a;
        score = s;
    }
    public void print()
    {
        Console.WriteLine("学号: {0}", num);
        Console.WriteLine("年龄: {0}", age);
        Console.WriteLine("成绩: {0}", score);
    }
    private int num, age, score;
}
class Program
{
    static void Main( )
    {
        Student[] stud = new Student[3] { new Student(1001, 18, 87), new Student(1002, 19, 76), new Student(1003, 18, 72) };
        for (int i = 0; i < stud.Length; i++)
        {
            stud[i].print();
        }
    }
}

原文地址:https://www.cnblogs.com/zhangyongjian/p/3623826.html