C#封装、继承

  //出生年龄必须大于0 
  //public int Age { get; set; }

        //这种封装可以修改属性的限制 propfull+TAB
        private int age;//private:私有  成员变量

        public int Age  //public:公共  属性
        {
            get { return age; }
            set {
                if (value> 0)
                {
                    age = value;
                }
                else
                {
                    Console.WriteLine("年龄必须大于等于0");
                }
            }
        }

继承中的重写

例子:

public virtual void Eat()//虚方法
{
    Console.WriteLine("吃白萝卜");
}
public override void Eat()//重写方法
{
    Console.WriteLine("吃肉");
}
 
/*   应用程序,该程序包括3个类:Monkey类、People类和主类E。要求: 
    (1) Monkey类中有个构造方法:Monkey (String s),并且有个public void speak()方法,在speak方法                中输出“咿咿呀呀……”的信息。 
    (2)People类是Monkey类的子类,在People类中定义方法speak(),在speak方法中输出“不错嘛!会说话了!”的信息。 
    (3)在People类中新增方法void think(),在think方法中输出“别说话!认真思考!”的信息。 
    (4)在主类E的main方法中创建Monkey与People类的对象类测试这2个类的功能。
*/
 class Program
     {
        static void Main(string[] args)
        {
            Mokey mo = new Mokey("Tom");
            mo.Speak();
            People po = new People("Helaa");
            po.Speak();
            po.Think();     
        }
     }
    class Mokey
    {
        public Mokey(String s)
        {
            this.name = s;
        }
        public string name;
        public void Speak()
        {
            Console.WriteLine("我是猴子" + name);
            Console.WriteLine("咿咿呀呀....");
        }
    }
    class Test3:Mokey
    {
        public Test3(string s):base(s)
        {
        }
        public new void Speak()
        {
             Console.WriteLine("我是人类"+name);
            Console.WriteLine("不错嘛!会说话了!");
        }
        public void Think()
        {
            Console.WriteLine("别说话!认真思考!");
        }
    }
}
/*
1)定义一个类,描述一个矩形,包含有长、宽两种属性,和计算面积方法。 
(2)编写一个类,继承自矩形类,长方体,具有长、宽、高属性,和计算体积的方法。 
(3)编写一个测试类,对以上两个类进行测试,创建一个长方体,定义其长、宽、高,输出其底面积和体积。
*/

    class Program
     {
        static void Main(string[] args)
        {
            Square s = new Square();
            Cub s1 = new Cub();
            s1.a = 10.0;
            s1.b = 12.0;
            s1.c = 5.0;
            s1.area();
            s1.Vol();
        }
     }
    class Square
    {
        public double a;
        public double b;
        public virtual void area()
        {
            Console.WriteLine("矩形的面积:"+(a*b));
        }
    }
    class Cub:Square
    {
        public double c;
        public override void area()
        {
            Console.WriteLine("长方体的底面积:{0:0.0}",(a*b));
        }
        public void Vol()
        {
            Console.WriteLine("长方体的体积:{0:0.0}",(a*b*c));
        }

    }
/*
设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight。 
小车类Car是Vehicle的子类,其中包含的属性有载人数loader。 
卡车类Truck是Car类的子类,其中包含的属性有载重量payload。 
每个类都有构造方法和输出相关数据的方法。 
最后,写一个测试类来测试这些类的功能。
*/

 class Program
     {
        static void Main(string[] args)
        {
            Car c1 = new Car(4,600,4);
            c1.Print();
            c1.Hold();
            Truck c2 = new Truck(6, 1000, 3, 800);
            c2.Print();
            c2.Hold();
            c2.Load();
        }
     }
 class Vehicle
    {
        int wheels;
        double weight;
        public Vehicle(int wheels,double weight)
        {
            this.weight = weight;
            this.wheels = wheels;
        }
        public virtual void Print()
        {
            Console.WriteLine("车轮的个数是:"+wheels+" "+"车重:{0:0.0}",weight);
        }
    }
    class Car:Vehicle
    {
        public int loader;
        public Car(int wheels, double weight,int loader) : base(wheels,weight)
        {
            this.loader = loader;
        }
        public override void Print()
        {
            base.Print();
        }
        public virtual void Hold()
        {
            Console.WriteLine("这辆车能载"+loader+"人");
        }
     }
    class Truck : Car
    {
        double payload;
        public Truck(int wheels, double weight, int loader, double payload) : base(wheels, weight,loader)
        {
            this.payload = payload;
        }
        public override void Print()
        {
            base.Print();
        }
        public override void Hold()
        {
            base.Hold();
        }
        public void Load()
        {
            Console.WriteLine("这辆车的载重是{0:0.0}",payload);
        }
    }

 

 

封装继承和多态是面向对象编程中的三个重要概念。下面是一个使用C#语言演示封装继承和多态的代码示例: 封装示例: ```csharp using System; namespace EncapsulationApplication { class Employee { private string name; private int age; public string Name { get { return name; } set { name = value; } } public int Age { get { return age; } set { age = value; } } public void Display() { Console.WriteLine("Name: " + name); Console.WriteLine("Age: " + age); } } class Program { static void Main(string[] args) { Employee emp = new Employee(); emp.Name = "John"; emp.Age = 30; emp.Display(); Console.ReadKey(); } } } ``` 在这个示例中,Employee封装了私有字段name和age,并通过公共属性Name和Age提供对它们的访问。Main方法中创建了一个Employee对象emp,并通过属性设置和Display方法展示了封装的效果。 继承示例: ```csharp using System; namespace InheritanceApplication { class Shape { protected int width; protected int height; public void setWidth(int w) { width = w; } public void setHeight(int h) { height = h; } } class Rectangle : Shape { public int getArea() { return width * height; } } class Program { static void Main(string[] args) { Rectangle rect = new Rectangle(); rect.setWidth(5); rect.setHeight(7); Console.WriteLine("Area: " + rect.getArea()); Console.ReadKey(); } } } ``` 在这个示例中,Shape作为基,Rectangle继承了Shape。Rectangle可以访问Shape的protected字段和方法。Main方法中创建了一个Rectangle对象rect,并通过调用setWidth、setHeight和getArea方法展示了继承的效果。 多态示例: ```csharp using System; namespace PolymorphismApplication { class Shape { public virtual void Draw() { Console.WriteLine("Drawing a shape"); } } class Circle : Shape { public override void Draw() { Console.WriteLine("Drawing a circle"); } } class Rectangle : Shape { public override void Draw() { Console.WriteLine("Drawing a rectangle"); } } class Program { static void Main(string[] args) { Shape[] shapes = new Shape[3]; shapes[0] = new Shape(); shapes[1] = new Circle(); shapes[2] = new Rectangle(); foreach (Shape shape in shapes) { shape.Draw(); } Console.ReadKey(); } } } ``` 在这个示例中,Shape定义了一个虚拟的Draw方法,Circle和Rectangle分别重写了这个方法。Main方法中创建了一个Shape型的数组,并分别用Shape、Circle和Rectangle的实例填充数组。通过遍历数组并调用Draw方法,展示了多态的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值