类的面向对象特性:封装,继承,多态

来源:互联网 发布:apache flink hdfs 编辑:程序博客网 时间:2024/05/21 17:14

封装:大多以类作为数据封装的基本单位

eg: class Program
    {
        static void Main(string[] args)
        {
            Computer mingri = new Computer();
        again:
            Console.WriteLine("请输入操作指令:");
            string str = Console.ReadLine();
            mingri.Open(str);
            Console.WriteLine("是否继续操作?");
            string isoperate = Console.ReadLine();
            if (isoperate == "true")
                goto again;
            Console.ReadLine();
        }
    }
    class Computer
    {
        public void Open(string str)
        {
            if (str == "启动")
            {
                string p;
                while (true)
                {
                    Console.WriteLine("请输入Power!");
                    p = Console.ReadLine();
                    if (p == "Power")
                    {
                        Console.WriteLine("电脑已启动!");
                        break;
                    }
                    else
                    {
                        Console.WriteLine("错误的指令,无法开启电脑,请重新输入!");
                    }
                }
            }
            else
            {
                Console.WriteLine("正在关闭电脑,请稍后!");
            }
        }
    }

类的继承:不能继承多个类      , 使用 base关键字调用基类的构造函数,使用 base 关键字调用父类的属性和方法

构造函数和析构函数不能被继承。

派生类如果定义了与继承而来的成员同名的新成员,就可以 覆盖已继承的成员。

eg1:

namespace 例8_15类的继承
{
    class Program
    {
        static void Main(string[] args)
        {
            Fruit fruit = new Fruit("水果");
            Apple apple = new Apple("苹果");
            fruit.Color = "无";
            apple.Color = "绿色";
            fruit.Information();
            apple.Information();
            Console.ReadLine();
        }
    }
    class Fruit
    {
        string name;
        string color;
        public Fruit(string str)
        {
            name = str;
        }
        public string Color
        {
            set { color = value; }
        }
        public void Information()
        {
            Console.WriteLine("{0} 颜色:{1}",name,color);
        }
    }
    class Apple : Fruit
    {
        string name;
        //string color;
        public Apple(string str)   //定义构造函数为name字段赋值
            : base(str)
        {
            name = str;
        }
    }
}

eg2:

public class Animal {
    public string name;
    public Animal(string name) {  
        this.name = name;
    }

public class Cat : Animal {
    public int age;
    public Cat(string name, int age): base(name) 
   {  
        this.age = age;
    }
}

this:指向类的当前实例
限定被相似的名称隐藏的成员
将对象作为参数传递到其他方法 
声明索引器


base:指向类的基类
调用基类上已被其他方法重写的方法
指定创建派生类实例时应调用的基类构造函数 

类的多态:

编写虚方法:基类的方法加上关键字virtual后变成虚方法,才可以被重写,从而实现面向对象最重要的特征--多态性,即基类可以使用派生类的方法。

eg:

public class Animal {
    public virtual voidEat() {  
        Console.WriteLine("Eat something!");
    }

public class Cat : Animal {
    public override voidEat() {   //完全取代基类代码
        Console.WriteLine("Eat small fishes!"); 
    }

new与override的区别

eg:

public class Animal {
    public virtual void Eat() {  
        Console.WriteLine("Eat something");
    }

public class Cat : Animal {
    public override void Eat() {  //完全取代基类方法
        Console.WriteLine("Eat small fishes!"); 
    }
}
public class Dog : Animal {
    public new void Eat() {    //暂时覆盖基类方法
        Console.WriteLine("Eat bones"); 
    }
}


Animal mycat=new Cat();
Animal mydog=new Dog();
mycat.Eat();
mydog.Eat();


eg1

namespace 例8_16.类的多态

{
    class Program
    {
        static void Main(string[] args)
        {
            Vehicle vehicle = new Vehicle();
            Train train = new Train();
            Car car = new Car();
            vehicle.Name = "交通工具";
            train.Name = "火车";
            car.Name = "汽车";
            vehicle.Move();
            train.Move();
            car.Move();
            Console.ReadLine();
        }
    }
    class Vehicle
    {
        string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public virtual void Move()
        {
            Console.WriteLine("{0}都可以移动",this.Name);
        }
    }
    class Train : Vehicle
    {
        public override void Move()
        {
            Console.WriteLine("{0}在铁轨上行驶",this.Name);
        }
    }
    class Car : Vehicle
    {
        public override void Move()
        {
            Console.WriteLine("{0}在公路上行驶",this.Name);
        }
    }
}


0 0