多态抽象类学习

  今天是忙碌的一天,早上六点半就起床然后跑操,然后早自习。第一节课本来没课却被老师硬生生的上了两节课(睡了过去),然后思政老师没来,放了昨天看的视频,中午真是生气学校搞什么藏龙之星的演讲,弄得一中午都不好,虽然逃课去吃了饭但是还是挺生气的。跟我半毛钱的关系都没有,我为什么要去,真是浪费我时间,搞得我最后还跟别人抱怨,搞得别人也生气,算了,就这样吧,感觉挺烦的。

  回寝室休息了十分钟又去就业课考试,学校的联通卡这个月居然只有600M流量,又超了,真的烦。考完试又来图书馆写不到一个小时的代码又要去上计算机课。唉。真的烦,真的不信这是大学。

 

好了,不抱怨了,贴代码吧,这是我今天在图书馆写的多态的抽象类学习部分代码。晚上把抽象类的练习做了,然后再把代码贴上来。就这样。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 抽象类
{
    class Program
    {
        static void Main(string[] args)
        {
            //狗狗会叫  猫咪会叫(>^ω^<)
            Animal a = new Cat();//创建子类对象赋值给父类
            a.Bark();//表现的父类类型,实际上被子类重写
            Console.ReadKey();
        }
    }
    public abstract class Animal//抽象类
        //不知道方法用抽象方法
        //知道方法就用虚方法
    {
        public abstract void Bark();//抽象方法,没有方法体
    }
    public class Dog:Animal
    {
        public override void Bark()
        {
            Console.WriteLine("汪汪汪");
        }
    }
    public class Cat:Animal
    {
        public override void Bark()
        {
            Console.WriteLine("喵喵喵");
        }
    }
}

  = =晚上,居然,没有写完= =还跳了好多,我以为时间够的,结果= =好吧,我吸取教训。明天不食言。就这样= =

 

2015/12/10更新

 

今天认真的学习了昨晚的内容,真的好难,然后今天写了很多注释。把上面的学动物叫添加了很多注释。嗯,还是要认真才行。好了,贴代码!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 抽象类
{
    /*
    整体思路
    1,虚方法
    2,抽象类

        虚方法----->父类Person里有自己的方法,虽然子类跟他不一样但是有共同特点
             ------>所以写了在父类里标记了自己的方法名一个virtual,紧接着在子类的方法名(void)之前标注一个override重新写入父类
        抽象类----->猫和狗索然都是动物但是自己的叫声都不一样,但是都会叫。---->所以,建立一个抽象的没有方法的类给子类重新写入=====》

    */
    class Program
    {
        static void Main(string[] args)
        {
            //狗狗会叫  猫咪会叫(>^ω^<)
            Animal a = new Cat();//创建子类对象赋值给父类
            a.Bark();//表现的父类类型,实际上被子类重写
            Console.ReadKey();
        }
    }
    public abstract class Animal
    //抽象类
    //不知道方法用抽象方法
    //知道方法就用虚方法
    {
        public virtual void TESTVITUAL()//父类无法实现这个虚方法
        {
            Console.WriteLine("我是虚方法");
        }
        public abstract void Bark();//抽象方法,没有方法体
        public abstract string Name//抽象成员
        {
            get;//抽象属性
            set;//子类必须实现抽象成员
        }
        //签名=返回值+参数
        //如果父类的抽象方法中有参数,那么继承这个抽象父类的子类在重写父类方法的时候必须传入对应的参数
        //声明明实例成员
        private int _age;
        public int Age
        {
            get { return _age; }
            set { _age = value; }
        }
        //写一个构造函数
        //先写一个空函数
        public Animal()
        {

        }
        //再写一个把外界传进来的name,age赋值给属性(属性保护字段)
        public Animal(string name, int age)
        {
            this.Age = age;
            this.Name = name;
        }
        //抽象类可以包含实例成员
        //但是实例类不能包含抽象成员
        //抽象类的成员可以不被子类实现
        //抽象类是有构造函数的,虽然不能被实例化
        public abstract string TestString(string name);

    }
    public abstract class Test : Animal
    {
        public abstract void test();

    }
    public class Dog : Animal
    {
        public override string Name
        {
            get
            {
                throw new NotImplementedException();
            }

            set
            {
                throw new NotImplementedException();
            }
        }

        public override void Bark()
        {
            Console.WriteLine("汪汪汪");
        }

        public override string TestString(string name)
        {
            throw new NotImplementedException();
        }
    }
    public class Cat : Animal
    {
        public override string Name
        {
            get
            {
                throw new NotImplementedException();
            }

            set
            {
                throw new NotImplementedException();
            }
        }
        public override void Bark()
        {
            Console.WriteLine("喵喵喵");
        }

        public override string TestString(string name)
        {
            throw new NotImplementedException();
        }
    }
}

  然后今天最难得还是运用抽象类来求一个图形的  面积   周长。抽象类大大的增加了代码的重复利用性,真的很好,虽然有点不适应但是慢慢来应该就会好的。

  1-------->在抽象类里先定义两个返回double值的方法,一个求面积  一个求周长 

  2-------->然后写了两个(一个给圆形用,一个给矩形用)的类,都继承上面那个抽象类。

  3-------->在自己求得图形里开始写字段,写属性保护字段(Ctrl+R+E),写构造函数把外界定义的参数传进去 给属性

  4-------->开始写子类自己的方法  开始把抽象类定义的两个方法写成“自己的”,根据自己的图形,在方法里写自己的计算公式。然后返回到main函数里

  5-------->在Main中就new 一个子类开始写入参数,然后写打印内容(求得的面积周长)

 

完。

 

好,上面说完了思路,下面就贴代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace P29抽象类练习_求面积和周长_
{
    class Program
    {
        static void Main(string[] args)
        {
            //使用多态求面积和周长以及圆形的面积和周长
            Shape shape = new Circle(5);
            double area = shape.GetArea();
            double perimeter = shape.GetPerimeter();
            Console.WriteLine("这个形状的面积是{0},周长是{1}", area, perimeter);
            Console.ReadKey();
        }
    }
    public abstract class Shape
    {
        public abstract double GetArea();//抽象方法
        public abstract double GetPerimeter();//返回double类型值
    }
    public class Circle : Shape//求圆的周长面积
    {
        private double _r;
        public double R
        {
            get { return _r; }
            set { _r = value; }
        }
        public Circle(double r)
        {
            this.R = r;
        }
        public override double GetArea()//求面积
        {
            return Math.PI * this.R * this.R;
        }
        public override double GetPerimeter()//求周长
        {
            return 2 * Math.PI * this.R;
        }
    }
    public class Square : Shape//求矩形的周长面积
    {
        private double _height;

        public double Height
        {
            get
            {
                return _height;
            }

            set
            {
                _height = value;
            }
        }
        private double _width;
        public double Width
        {
            get
            {
                return _width;
            }

            set
            {
                _width = value;
            }
        }
        public Square(double height, double width)
        {
            this.Height = height;
            this.Width = width;
        }
        public override double GetArea()
        {
            return this.Height * this.Width;
        }
        public override double GetPerimeter()
        {
            return (this.Height + this.Width) * 2;
        }

    }

}

  

 

posted @ 2015-12-09 16:07  老李拉面馆  阅读(293)  评论(0编辑  收藏  举报