类、抽象类,接口


1.声明类
类使用 class 关键字进行声明
//访问级别 关键字 类名称
public class Customer
    {
        //类成员(字段、属性、方法、事件) 
}
2.创建对象
类定义对象的类型,但它不是对象本身。 
对象是基于类的具体实体,有时称为类的实例。
通过使用 new 关键字(后跟对象将基于的类的名称)可以创建对象
Customer object1 = new Customer();
创建了两个对象引用,它们引用同一个对象。 因此,通过 object3 对对象所做的任何更改都将反映在随后使用的 object4 中。 由于基于类的对象是按引用来引用的,因此类称为引用类型。
Customer object3 = new Customer();
Customer object4 = object3;
抽象类
含有abstract修饰符的class即为抽象类,抽象类是特殊的类,只是不能被实例化,可以创建一个变量,其类型是一个抽象类,并让它指向具体子类的一个实例;除此以外,具有类的其他特性;重要的是抽象类可以包括抽象方法,这是普通类所不能的。抽象方法只能声明于抽象类中,且不包含任何实现,派生类必须覆盖它们。另外,抽象类可以派生自一个抽象类,可以覆盖基类的抽象方法也可以不覆盖。
语法格式为:
      访问修饰符  abstract  class 类名:基类或接口
      {
              类成员;
       }
       声明抽象类时,除abstract关键字,class关键字和类名外,其他的都是可选项。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace AbstractExample
{
    abstract class A//抽象类
    {
        public abstract void add(int a,int b);//抽象方法
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace AbstractExample
{
    class B : A//派生类B
    {
        public override void add(int a, int b)//重写抽象方法
        {
            int sum = a + b;
            Console.WriteLine(sum);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace AbstractExample
{
    class C : A//派生类C
    {
        public override void add(int a, int b)//重写抽象方法
        {
            int sum = System.Math.Abs(a + b);//取两数和的绝对值
            Console.WriteLine(sum);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace AbstractExample
{
    class Program
    {
        static void Main(string[] args)
        {
            B b = new B();
            b.add(1,-2);
            C c = new C();
            c.add(1,-2);
            Console.ReadLine();
        }
    }
}
接口
接口就是定义一个协定,只是一个架子,必须由结构或者类来实现。
interface 接口名
{
    方法声明;
}
1.接口用于描述一组类的公共方法/公共属性. 它不实现任何的方法或属性,只是告诉继承它的类至少要实现哪些功能,继承它的类可以增加自己的方法.
2.使用接口可以使继承它的类: 命名统一/规范,易于维护.比如: 两个类 "狗"和"猫",如果它们都继承了接口"动物",其中动物里面有个方法Behavior(),那么狗和猫必须得实现Behavior()方法,
并且都命名为Behavior这样就不会出现命名太杂乱的现象.如果命名不是Behavior(),接口会约束即不按接口约束命名编译不会通过.
3.提供永远的接口。 当类增加时,现有接口方法能够满足继承类中的大多数方法,没必要重新给新类设计一组方法,也节省了代码,提高了开发效率.
Program.cs
namespace InterfaceExample
{
    class Program
    {
        static void Main(string[] args)
        {
            //不同类的相同方法名输出不同东西
            Dog myDog = new Dog();
            myDog.Behavior(); //输出: "晚上睡觉,白天活动"
            Cat myCat = new Cat();
            myCat.Behavior(); //输出: "白天睡觉,晚上活动"
        }
    }
}
IAnimal.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace InterfaceExample
{
    //公共接口:动物
    interface IAnimal
    {
        void Behavior();//行为方法:描述动物特性
    }
}
Dog.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace InterfaceExample
{
    class Dog : IAnimal
    {
        string ActiveTime = "白天";
        public void Behavior()
        {
            Console.WriteLine("晚上睡觉,白天活动");
        }
    }
}
Cat.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace InterfaceExample
{
    class Cat : IAnimal
    {
        string ActiveTime = "夜晚";
        public void Behavior()
        {
            Console.WriteLine("白天睡觉,晚上活动");
        }
    }
}
接口和抽象类异同
接口是引用类型的,类似于类,和抽象类的相似之处有三点:
1、不能实例化;
2、包含未实现的方法声明;
3、派生类必须实现未实现的方法,抽象类是抽象方法,接口则是所有成员(不仅是方法包括其他成员);
两者的语法区别:
1.抽象类可以有构造方法,接口中不能有构造方法。
2.抽象类中可以有普通成员变量,接口中没有普通成员变量
3.抽象类中可以包含非抽象的普通方法,接口中的所有方法必须都是抽象的,不能有非抽象的普通方法。
4. 抽象类中的抽象方法的访问类型可以是public,protected,但接口中的抽象方法只能是public类型的,并且默认即为public abstract类型。
5. 抽象类中可以包含静态方法,接口中不能包含静态方法
6. 抽象类和接口中都可以包含静态成员变量,抽象类中的静态成员变量的访问类型可以任意,但接口中定义的变量只能是public static final类型,并且默认即为public static final类型。
7. 一个类可以实现多个接口,但只能继承一个抽象类。
8.接口可以用于支持回调,而继承并不具备这个特点.    
9.抽象类实现的具体方法默认为虚的,但实现接口的类中的接口方法却默认为非虚的,当然您也可以声明为虚的. 
抽象类和接口的使用:
1.如果预计要创建组件的多个版本,则创建抽象类。抽象类提供简单的方法来控制组件版本。
2.如果创建的功能将在大范围的全异对象间使用,则使用接口。如果要设计小而简练的功能块,则使用接口。
3.如果要设计大的功能单元,则使用抽象类.如果要在组件的所有实现间提供通用的已实现功能,则使用抽象类。
4.抽象类主要用于关系密切的对象(当在共性较多的对象间寻求功能上的差异时,使用抽象基类。);而接口适合为不相关的类提供通用功能(当在差异较大的对象间寻求功能上的共性时,使用接口。)。
5.好的接口定义应该是具有专一功能性的,而不是多功能的,否则造成接口污染。如果一个类只是实现了这个接口的中一个功能,而不得不去实现接口中的其他方法,就叫接口污染。
实例1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace AbstractInterface
{
    class Program
    {
        static void Main(string[] args)
        {
            //抽象类
            Animal duck = new Duck("Duck");
            duck.MakeVoice();
            duck.Show();
            Animal dog = new Dog("Dog");
            dog.MakeVoice();
            dog.Show();
            //接口
            IAction dogAction = new Dog("A big dog");
            dogAction.Move();
        }
    }
}


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


namespace AbstractInterface
{
    //定义抽象类
    abstract class Animal
    {
        protected string _name;
        //声明抽象属性
        public abstract string Name { get; }
        //声明抽象方法
        public abstract void Show();
        //实现一般方法
        public void MakeVoice()
        {
            Console.WriteLine("All animals can make voice!");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace AbstractInterface
{
    interface IAction
    {
        //定义公共方法标签
        void Move();
    }
}


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


namespace AbstractInterface
{
    //实现抽象类和接口
    class Duck : Animal,IAction
    {
        public Duck(string name)
        {
            _name = name;
        }
        //重载抽象方法
        public override void Show()
        {
            Console.WriteLine(_name + " is showing for you.");
        }
        //重载抽象属性
        public override string Name
        {
            get { return _name; }
        }
        //实现接口方法
        public void Move()
        {
            Console.WriteLine(_name + " also can swim.");
        }
    }
}


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


namespace AbstractInterface
{
     //实现抽象类和接口
    class Dog : Animal, IAction
    {
        public Dog(string name)
        {
            _name = name;
        }
        //重载抽象方法
        public override void Show()
        {
            Console.WriteLine(_name + " is showing for you.");
        }
        //重载抽象属性
        public override string Name
        {
            get { return _name; }
        }
        //实现接口方法
        public void Move()
        {
            Console.WriteLine(_name + " also can run.");
        }
    }
}
实例2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace AbstractInterface1
{
    //接口
    interface IAnimal
    {
        //接口中声明的方法,这里不可用static,abstract修饰
        void eating();
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace AbstractInterface1
{
    //抽象类Bird继承了IAnomal接口
    abstract class Bird : IAnimal
    {
        public void eating()
        {
            //抽象类实现接口的方法且必须实现该方法
            Console.WriteLine("eat!");
        }
        //定义抽象方法
        abstract public void flying();
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace AbstractInterface1
{
    //Chick类是抽象类Bird类的派生类
    class Chick :Bird
    {
        public int wingnum;
        public Chick()
        {
            Console.WriteLine("This is a chick");
            wingnum = 2;
        }
        //实现抽象类方法
        public override void flying()
        {
            Console.WriteLine("The chick can fly!");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace AbstractInterface1
{
    //声明抽象类A
    abstract class A
    {
        public int x;//普通公有成员
        //抽象类A的构造函数
        public A(int m)
        {
            x = m;
        }
        //抽象方法
        abstract public int fun1();
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace AbstractInterface1
{
    //类C继承于抽象类B,必须实现所有抽象方法
    class C :B 
    {
        int y, z;
        //构造函数
        public C(int x1, int y1): base(x1)
        {
            y = x1;
            z = y1;
        }
        //抽象方法fun1实现
        public override int fun1()
        {
            return x * y;
        }
        //抽象方法fun2实现
        public override int fun2()
        {
            return x / y;
        }
        //抽象属性px的实现
        public override int px
        {
            get
            {
                try { return x + 10; }
                catch 
                { 
                    throw new Exception("The method or operation is not implemented."); 
                }
            }
            set
            {
                 try { x = value; }
                 catch 
                 { 
                     throw new Exception("The method or operation is not implemented."); 
                 }
            }
        }
        public override int py  //抽象属性py的实现
        {
            get
            {
                try  { return y-10; }
                catch
                { 
                    throw new Exception("The method or operation is not implemented."); 
                }
            }
        }
    }
}


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


namespace AbstractInterface1
{
    //抽象类B继承抽象类A
    abstract class B : A
    {
        //抽象类A的构造函数
        public B(int m):base(1)
        {
            x = m;
        }
        abstract public int fun2();
        public abstract int px { set; get; }
        public abstract int py { get; }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace AbstractInterface1
{
    class Program
    {
        static void Main(string[] args)
        {
            //类的使用
            Chick ch = new Chick();
            ch.eating();
            ch.flying();
            Console.WriteLine("翅膀个数是:{0}", ch.wingnum);
            //多层次抽象类举例
            C c = new C(5, 2); //创建类c的对象C
            Console.WriteLine("{0},{1}", c.fun1(), c.fun2());
            Console.WriteLine("{0}", c.x);
            Console.WriteLine("{0},{1}", c.px, c.py); //调用抽象属性
        }
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值