抽象类中的抽象方法禁止写任何实现
1,,a:抽象类不能被继承
b:抽象方法不能有任何实现,一切实现都在子类中
c:抽象类中可以有非抽象方法,但是非抽象类绝对不能有抽象方法
class Program
{
static void Main(string[] args)
{
Person a = new Chinese();
Person b = new Japnese();
a.A();
b.A();
Console.ReadKey();
}
}
public abstract class Person
{
public abstract void A();
}
public class Chinese : Person
{
public override void A()
{
Console.WriteLine("dd");
}
}
public class Japnese : Person
{
public override void A()
{
Console.WriteLine("ww");
}
}
2,以前那个动物类,动物Animal 都有吃Eat和叫Bark的方法,狗Dog和猫Cat叫的方法不一样.父类中没有默认的实现所哟考虑用抽象方法。
class Program
{
static void Main(string[] args)
{
Animal a = new Dog();
Animal b = new Cat();
a.Eat();
b.Bark();
Console.ReadKey();
}
}
public abstract class Animal
{
public abstract void Eat();
public abstract void Bark();
}
public class Cat:Animal
{
public override void Eat()
{
Console.WriteLine(“猫吃鱼”);
}
public override void Bark()
{
Console.WriteLine("喵喵");
}
}
public class Dog : Animal
{
public override void Eat()
{
Console.WriteLine("狗吃骨头");
}
public override void Bark()
{
Console.WriteLine("汪汪");
}
}
3,计算形状Shape(圆Circle,矩形Square?,正方形Rectangle)的面积、周长
class Program
{
static void Main(string[] args)
{
Shape d = new Circle(5);
Console.WriteLine(d.Mianji());
double s1 = Convert.ToDouble(Console.ReadLine());
double s2 = Convert.ToDouble(Console.ReadLine());
Shape a = new Square(s1,s2);
Console.WriteLine(a.Zhoucahng());
Console.ReadLine();
}
}
public abstract class Shape //如果这个类没加public,那么默认就是internal,那么子类如果加public的话,就会报错(子类比父类的课访问性低),虽然说public、internal两者之间没什么可比性,但是个人认为public 比internal管的宽
{
public abstract double Mianji();
public abstract double Zhoucahng();
}
class Circle : Shape
{
public Circle(double r)
{
this.Banjing = r;
}
public double Banjing
{
get;
set;
}
public override double Mianji()//括号里面千万别写参数,务必与父类中的构造函数一样
{
return Math.PI * Banjing * Banjing;
}
public override double Zhoucahng()
{
return Math.PI * Banjing * 2; //用的时候还是用属性,不是那个构造函数的。注意
}
}
public class Square : Shape
{
public Square(double a, double b)
{
this.A = a;
this.B = b;
}
public double A
{
get;
set;
}
public double B
{
get;
set;
}
public override double Mianji()
{
return A * B;
}
public override double Zhoucahng()
{
return 2 * (A + B);
}
}
4,class Program
{ //要实现U盘、MP3播放器、移动硬盘三种移动存储设备,要求计算机能同这三种设备进行数据交换,并且以后可能会有新的第三方的移动存储设备,所以计算机必须有扩展性,能与目前未知而以后可能会出现的存储设备进行数据交换。各个存储设备间读、写的实现方法不同,U盘和移动硬盘只有这两个方法,MP3Player还有一个PlayMusic方法
static void Main(string[] args)
{
Upan u = new Upan();
Yingpan y = new Yingpan();
MP3 m = new MP3();
Computer c = new Computer();
c.Disk = u; //把u盘插进来了。把u改成m,MP3九插进来了
c.ReadData();
c.WriteData();
Console.ReadKey();
}
}
public class Computer
{
public Mobio Disk
{
get;
set;
}
public void ReadData()
{
Disk.Read();
}
public void WriteData()
{
Disk.Write();
}
}
public abstract class Mobio
{
public abstract void Read();
public abstract void Write();
}
public class Upan : Mobio
{
public override void Read()
{
Console.WriteLine(“U盘在读”);
}
public override void Write()
{
Console.WriteLine("U盘在写");
}
}
public class MP3 : Mobio
{
public override void Read()
{
Console.WriteLine("MP3在读");
}
public override void Write()
{
Console.WriteLine("MP3在写");
}
public void PlayMusic()
{
Console.WriteLine("MP3用来听歌");
}
}
public class Yingpan : Mobio
{
public override void Read()
{
Console.WriteLine("硬盘在读");
}
public override void Write()
{
Console.WriteLine("硬盘在写");
}
}
5, class Program
{//橡皮鸭子(RubberDuck)、真实的鸭子(RealDuck)。两个鸭子都会游泳,而橡皮鸭子和真实的鸭子都会叫,只是叫声不一样,橡皮鸭子“唧唧”叫,真实地鸭子“嘎嘎”叫
static void Main(string[] args)
{
Duck d = new RubberDuck();
d.Youyong();
d.Jiao();
Console.ReadKey();
}
}
public abstract class Duck
{
public void Youyong()
{
Console.WriteLine(“鸭子在游泳”);
}
public abstract void Jiao();
}
public class RubberDuck:Duck
{
public override void Jiao()
{
Console.WriteLine(“橡皮鸭子唧唧叫”);
}
}
public class RealDuck:Duck
{
public override void Jiao()
{
Console.WriteLine("真鸭子嘎嘎叫");
}
}
6,最简单的代码实现接口
1,接口声明不能含有任何访问修饰符,接口中的方法、属性也是
2,接口中的方法不能有任何实现
class Program
{
static void Main(string[] args)
{
A p = new C();
p.B();
Console.ReadKey();
}
}
interface A
{
void B();
String Name
{
get;
set;
}
}
class C:A
{
public void B()
{
Console.WriteLine(“我我”);
}
public string Name
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
}
7,简单接口
1,接口可以实现多重继承,这点类是办不到的
class Program
{
static void Main(string[] args)
{
Fly A = new Bird();
Fly B = new Plane();
Anmial C = new Bird();
C.Eat();
B.Fly();
A.Fly();
Console.ReadKey();
}
}
class Anmial
{
public void Eat()
{
Console.WriteLine(“动物吃东西”);
}
}
interface Fly
{
void Fly();
}
class Bird : Anmial, Fly
{
public void Fly()
{
Console.WriteLine(“小鸟飞在天空中”);
}
}
class Plane : Fly
{
public void Fly()
{
Console.WriteLine(“飞机飞在天空中”);
}
}
8,class Program
{//登记方法(接口作为参数,实现多态)
DengJi(Person person) //人口登记,Person是抽象类
DengJi(Car car) //汽车也要登记
DengJi(House house) //房子也要登记。 IJiKou
DengJi(IJieKou jk);
static void Main(string[] args)
{
Chinese c = new Chinese();
Japnese j = new Japnese();
Car car = new Car();
A( j);
Console.ReadKey();
}
public static void A(Jie B)
{
Console.WriteLine("那你看什么,,");
B.Show();
}
}
public class Person
{
public String Name
{
get;
set;
}
public virtual void Show()
{
}
}
interface Jie
{
void Show();
}
public class Chinese : Person,Jie
{
public override void Show()
{
Console.WriteLine("我是中国人,,");
}
void Jie.Show()
{
Console.WriteLine("中国人在玩手机,,");
}
}
public class Japnese : Person,Jie
{
public override void Show()
{
Console.WriteLine("我是日本人,,,");
}
void Jie.Show()
{
Console.WriteLine("日本人在看报纸,,,");
}
}
public class Car : Jie
{
public void Show()
{
Console.WriteLine("我是汽车");
}
}