使用类来继承接口

如public class Car : MotorVehicle, IDrivable, ISteerable形式,例如:

 
  
1 /*
2 Example8_3.cs illustrates inheriting from a class and
3 implementing multiple interfaces
4   */
5
6 using System;
7
8
9 public interface IDrivable
10 {
11
12 // method declarations
13 void Start();
14 void Stop();
15
16 // property declaration
17 bool Started
18 {
19 get ;
20 }
21
22 }
23
24
25 public interface ISteerable
26 {
27
28 // method declarations
29 void TurnLeft();
30 void TurnRight();
31
32 }
33
34
35 public class MotorVehicle
36 {
37
38 // declare the field
39 private string model;
40
41 // define a constructor
42 public MotorVehicle( string model)
43 {
44 this .model = model;
45 }
46
47 // declare a property
48 public string Model
49 {
50 get
51 {
52 return model;
53 }
54 set
55 {
56 model = value;
57 }
58 }
59
60 }
61
62
63 // Car class inherits from the MotorVehicle class and
64 // implements the IDrivable and ISteerable interfaces
65 public class Car : MotorVehicle, IDrivable, ISteerable
66 {
67
68 // declare the underlying field used by the
69 // Started property of the IDrivable interface
70 private bool started = false ;
71
72 // define a constructor
73 public Car( string model) :
74 base (model) // calls the base class constructor
75 {
76 // do nothing
77 }
78
79 // implement the Start() method of the IDrivable interface
80 public void Start()
81 {
82 Console.WriteLine( " car started " );
83 started = true ;
84 }
85
86 // implement the Stop() methodof the IDrivable interface
87 public void Stop()
88 {
89 Console.WriteLine( " car stopped " );
90 started = false ;
91 }
92
93 // implement the Started property of the IDrivable interface
94 public bool Started
95 {
96 get
97 {
98 return started;
99 }
100 }
101
102 // implement the TurnLeft() method of the ISteerable interface
103 public void TurnLeft()
104 {
105 Console.WriteLine( " car turning left " );
106 }
107
108 // implement the TurnRight() method of the ISteerable interface
109 public void TurnRight()
110 {
111 Console.WriteLine( " car turning right " );
112 }
113
114 }
115
116
117 class Example8_3
118 {
119
120 public static void Main()
121 {
122
123 // create a Car object
124 Car myCar = new Car( " MR2 " );
125
126 Console.WriteLine( " myCar.Model = " + myCar.Model);
127
128 // call myCar.Start()
129 Console.WriteLine( " Calling myCar.Start() " );
130 myCar.Start();
131
132 // call myCar.TurnLeft()
133 Console.WriteLine( " Calling myCar.TurnLeft() " );
134 myCar.TurnLeft();
135
136 }
137
138 }
开课实验室:现代信息交流中心403 开课时间: 2019年**月**日 实验报告: 2019年 11月 9日 "学院 "信息工程学"年级、 "软件1803班 "学号 "20180822 " "名称 "院 "专业、 " " " " " " "班 " " " " "实验型 "验证 综合 设计 创新 "成绩 " " "教" " "师"教师签名: " "评"年 月 日 " "语" " 1. 实验目的:熟悉和掌握Java中的面向对象编程,包括继承和多态等面向对象方法 。 2. 实验内容: 1. 编写一个圆Circle,该拥有: (1)属性radius; (2)构造方法Circle()和Circle(double r) (3)成员方法: double getArea() double getPerimeter() void show()//显示半径、周长和面积 编写一个圆柱体Cylinder,它继承上面的Circle,还拥有: (1)属性hight (2)构造方法Cylinder(double r, double h) (3)成员方法 double getVolume() void showVolume() 编写测试,创建上面两个的对象,并运行其中的方法。 2. 创建一个Vehicle并将它声明为抽象。在Vehicle中声明一个NumOfWheels方法,用 于显示车轮的数量。 创建两个Car和Motorbike从Vehicle继承,并在这两个中实现NumOfWheels方法。 在Car中,应当显示"四轮车"信息; 而在Motorbike中,应当显示"双轮车"信息。 创建一个带main方法的,在该中创建Car和Motorbike的实例,并在控制台中显示消 息。 3. 采用继承接口等面向对象思维来设计教练和运动员: (1)乒乓球运动员和篮球运动员 (2)乒乓球教练和篮球教练 (3)为了出国交流,跟乒乓球相关的人员都需要学习英语。 提示:重点考虑中属性、方法和间层次关系,并编程实现。 3. 源代码清单: 1.源代码清单 package text1; public class Circle { double radius; Circle(){ radius=0; } Circle(double radius){ this.radius=radius; } double getArea(){ return radius*radius*Math.PI; } double getPerimeter(){ return radius*Math.PI*2; } void show(){ System.out.println("半径:"+radius+"周长:"+getPerimeter()+"面积:"+getArea( )); } } public class Cylinder extends Circle{ double hight; Cylinder(double r, double h){ this.radius=r; this.hight=h; } double getVolume(){ return getArea()*hight; } void showVolume(){ System.out.println("体积:"+getVolume()); } public static void main(String args[]) { Circle a = new Circle(4); Cylinder b= new Cylinder(4,6); a.show(); b.showVolume(); } } 2.源代码清单 package text1; abstract class Vehicle { abstract void NumOfWheels(); } public class Car extends Vehicle{ void NumOfWheels(){ System.out.println("四轮车"); } } public class Motorbike extends Vehicle{ void NumOfWheels(){ System.out.println("双轮车"); } public static void main(String args[]){ Car a = new Car(); Motorbike b= new Motorbike(); a.NumOfWheels(); b.NumOfWheels(); } } 3. 源代码清单 package shiyan3; abstract class person { public void show() { Syst
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值