实验六继承
实验六继承
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace shape
{
publicabstractclass shape
{
publicabstractdouble Area();
}
publicclass Circle:shape//建立Circle的派生类
{
privatedouble r;
publicdouble R
{
get
{
return r;
}
set
{
r = value;
}
}
public Circle(double r)
{
this.r = r;
}
publicoverridedouble Area()
{
return Math.PI * r * r;
}
}
publicclass Rectangle: shape//建立Rectangle的派生类 {
privatedouble a;
publicdouble A
{
get
{
return a;
}
set
{
a = value;
}
}
privatedouble b;
publicdouble B
{
get
{
return b;
}
set
{
b = value;
}
}
public Rectangle(double a,double b)
{
this.a = a;
this.b = b;
}
publicoverridedouble Area()
{
return a * b;
}
}
class Program
{
staticvoid Main(string[] args)
{
Console.WriteLine("请输入圆的半径:");
double r = double.Parse(Console.ReadLine()); shape s = new Circle(r);
Console.WriteLine("圆的面积是:{0}",s.Area()); Console.WriteLine("请输入矩形的长和宽:"); double a = double.Parse(Console.ReadLine()); double b = double.Parse(Console.ReadLine()); shape f = new Rectangle(a, b);
Console.WriteLine("矩形的面积是:{0}",f.Area()); Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic; using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SchoolManagement
{
publicclass User
{
privatestring userName; publicstring Name
{
get
{
return userName;
}
set
{
userName = value; }
}
privatestring password; publicstring Password
{
get
{
return password;
}
set
{
password = value;
}
}
privatestring userType;
publicstring Usertype
{
get
{
return userType;
}
set
{
userType = value;
}
}
publicvoid Login()
{
if (userName.Equals("1")&&password.Equals("1")) {
DisplayPersonInfo();
}
}
publicvirtualvoid DisplayPersonInfo()
{
Console.WriteLine("姓名:{0}",userName); Console.WriteLine("用户类型:{0}",userType);
}
publicclass Student : User
{
privatestring classID;
publicstring ClassID
{
get
{
return classID;
}
set
{
classID = value;
}
}
privatefloat stuScore;
publicfloat StuScore
{
get
{
return stuScore;
}
set
{
stuScore = value;
}
}
public Student()
{
userType = "学生";
}
publicoverridevoid DisplayPersonInfo()
{
base.DisplayPersonInfo();
Console.WriteLine("班级:{0}", classID);
}
publicvoid CheckScore()
{
Console.WriteLine("成绩:{0}", this.stuScore); }
}
}
publicclass Teacher : User
{
privatestring department; publicstring Department
{
get
{
return department;
}
set
{
department = value;
}
}
privatefloat salary;
publicfloat Salary
{
get
{
return salary;
}
set
{
salary = value;
}
}
public Teacher()
{
Usertype = "教师";
}
publicoverridevoid DisplayPersonInfo()
{
base.DisplayPersonInfo();
Console.WriteLine("部门:" + this.department); }
publicvoid checkSalary()
{
Console.WriteLine("工资:" + this.salary);
}
publicclass SysManager : User
{
public SysManager()
{
Usertype= "管理员";
}
public Student InsertStudent()
{
Student stu= new Student();
Console.Write("请输入人员类型");
string Usertype = Console.ReadLine(); Console.Write("请输入学生姓名");
string stuname = Console.ReadLine(); Console.Write("请输入学生ID");
string ClassID = Console.ReadLine();
https://www.360docs.net/doc/529411046.html, = stuname;
stu.ClassID = ClassID;
stu.StuScore = 70;
return stu;
}
public Teacher InsertTeacher()
{
Teacher teacher = new Teacher();
Console.Write("请输入教师姓名");
string teachname = Console.ReadLine();
Console.Write("请输入教师所属学院");
string Department = Console.ReadLine();
https://www.360docs.net/doc/529411046.html, = teachname;
teacher.Department = Department;
teacher.Salary = 5000;
return teacher;
}
}
}
class Program
{
staticvoid Main(string[] args)
{
Teacher.SysManager sys = new Teacher.SysManager();
https://www.360docs.net/doc/529411046.html, = "1";
sys.Password = "1";
sys.Login();
Console.WriteLine("------------------------------"); Teacher teacher = sys.InsertTeacher();
User.Student stu = sys.InsertStudent();
teacher.DisplayPersonInfo();
teacher.checkSalary();
Console.WriteLine("------------------------------"); stu.DisplayPersonInfo();
stu.CheckScore();
Console.WriteLine("------------------------------"); }
}
}
实验七多态
上述过程实现的是动态联编,只有当程序运行时,才知道结果。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace transportation
{
publicclass Plane:Tool
{
public Plane()
{
ToolType = "plane";
}
publicoverridevoid Select()
{
Console.WriteLine("选择的交通工具:飞机。"); }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace transportation
{
publicclass Car :Tool
{
public Car()
{
ToolType = "car";
}
publicoverridevoid Select()
{
Console.WriteLine("选择的交通工具:汽车"); }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace transportation
{
publicclass Train:Tool
{
public Train()
{
ToolType = "train";
}
publicoverridevoid Select()
{
Console.WriteLine("选择的交通工具:火车"); }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace transportation
{
publicabstractclass Tool
{
privatestring toolType;
publicstring ToolType
get
{
return toolType;
}
set
{
toolType = value;
}
}
publicabstractvoid Select();
}
enum toolType
{
car,
train,
plane
}
class Program
{
staticvoid Main(string[] args)
{
Random rnd = new Random();
Tool tool = null;
toolType type = (toolType)rnd.Next(3); switch(type)
{
case toolType.car:
tool = new Car(); break;
case toolType.train:
tool = new Train(); break;
case toolType.plane:
tool = new Plane(); break;
}
if (tool != null)
tool.Select(); Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic; using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Game
{
class Program
{
staticvoid Main(string[] args)
{
Monster player = new Human();
https://www.360docs.net/doc/529411046.html, = "天一"; player.HP = 3000;
player.MP = 1500;
player.Accuracy = 80; player.Attack = 300; player.Defense = 100; player.Dodge = 50;
player.DoAttack();
player.DoStand();
player.DisplayInfo(); }
}
}
using System;
using System.Collections.Generic; using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Game
{
publicinterface Monster
{
string Name
{
get;
set;
}
int HP
get;
set;
}
int MP
{
get;
set;
}
int Defense
{
get;
set;
}
int Attack
{
get;
set;
}
int Dodge
{
get;
set;
}
int Accuracy
{
get;
set;
}
void DoAttack();
void DoStand();
void DoDefense();
void DisplayInfo();
}
}
using System;
using System.Collections.Generic; using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Game
{
publicclass Repeat
{
privateint healthPower; privateint magicPower; privateint defense; privateint attack;
privateint dodge;
privateint accuracy; privatestring name;
publicint HP
{
get { return healthPower; } set { healthPower = value; } }
publicint MP
{
get { return magicPower; } set { magicPower = value; } }
publicint Defense
{
get { return defense; }
set { defense = value; }
}
publicint Attack
{
get { return attack; }
set { attack = value; }
}
publicint Dodge
{
get { return dodge; }
set { dodge = value; }
}
publicint Accuracy
{
get { return accuracy; }
set { accuracy = value; }
}
publicstring Name
{
get { return name; }
set { name = value; }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Game
{
publicclass Beast : Repeat, Monster
{
publicvoid DoAttack()
{
Console.WriteLine(Name + "的爪子狠狠的一抓,你的头皮去了好大一块!"); }
publicvoid DoStand()
{
Console.WriteLine(Name + "四脚着地,目露凶光,你看到它时不寒而栗!"); }
publicvoid DoDefense()
{
Console.WriteLine(Name + "向你大声咆哮,吓得你抱头鼠窜。");
}
publicvoid DisplayInfo()
{
Console.WriteLine("姓名:" + Name);
Console.WriteLine("种族:四足兽类");
Console.WriteLine("-------------------------------------"); Console.WriteLine("状态");
Console.WriteLine("生命:{0}\t\t魔法:{1}", HP, MP);
Console.WriteLine("-------------------------------------"); Console.WriteLine("战斗力");
Console.WriteLine("攻击:{0}\t\t防御:{1}", Attack, Defense); Console.WriteLine("命中:{0}\t\t\t闪避:{1}", Accuracy, Dodge);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Game
{
class Human : Repeat, Monster
{
publicvoid DoAttack()
{
Console.WriteLine(Name + "的攻击动作很华丽!===!=========>");
}
publicvoid DoStand()
{
Console.WriteLine(Name + "站在那里,果然是伟岸英挺,气宇轩昂,简直是人中龙凤!"); }
publicvoid DoDefense()
{
Console.WriteLine(Name + "将手中兵器舞成一片,格开了所有人的进攻。");
}
publicvoid DisplayInfo()
{
Console.WriteLine("姓名:" + Name);
Console.WriteLine("种族:人类");
Console.WriteLine("-------------------------------------");
Console.WriteLine("状态");
Console.WriteLine("生命:{0}\t\t魔法:{1}", HP, MP);
Console.WriteLine("-------------------------------------");
Console.WriteLine("战斗力");
Console.WriteLine("攻击:{0}\t\t防御:{1}", Attack, Defense);
Console.WriteLine("命中:{0}\t\t\t闪避:{1}", Accuracy, Dodge);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Teris
{
enum ShapeType
{
Block = 0,
L,
T,
Z,
}
class Program
{
staticvoid Main(string[] args)
{
Random rnd = new Random();
while (true)
{
Shape shape = null;
ShapeType type = (ShapeType)rnd.Next(4);
switch (type)
{
case ShapeType.Block:
shape = new ShapeBlock();
break;
case ShapeType.L:
shape = new ShapeL();
break;
case ShapeType.T:
shape = new ShapeT();
break;
case ShapeType.Z:
shape = new ShapeZ();
break;
}
if (shape != null)
{
shape.DisplayInfo();
shape.Draw();
Console.WriteLine("-------------------------");
System.Threading.Thread.Sleep(1000); }
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Teris
{
publicinterface Shape
{
string ShapeType
{
get;
set;
}
void DisplayInfo();
void Draw();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Teris
{
publicclass ShapeBlock:Shape
{
privatestring shapeType;
publicstring ShapeType
{
get { return shapeType; }
set { shapeType = value; }
}
public ShapeBlock()
{
ShapeType = "田形";
}
publicvoid DisplayInfo()
{
Console.WriteLine("当前图形类型" + shapeType); }
publicvoid Draw()
{
Console.WriteLine("_________");
Console.WriteLine("| | |");
Console.WriteLine("| | |");
Console.WriteLine("_________");
Console.WriteLine("| | |");
Console.WriteLine("| | |");
Console.WriteLine("_________");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Teris
{
publicclass ShapeL:Shape
{
privatestring shapeType;
publicstring ShapeType
{
get { return shapeType; }
set { shapeType = value; }
}
public ShapeL()
{
ShapeType = "L形";
}
publicvoid DisplayInfo()
{
Console.WriteLine("当前图形类型" + shapeType); }
publicvoid Draw()
{
Console.WriteLine("|");
Console.WriteLine("|");
Console.Write("|");
Console.WriteLine("__");
}
}
}
java继承与接口实验三
信息与计算科学专业实验报告 课程名称Java课程设计总实验学时: 16 第3 次共6 次 实验项目名称继承与接口本次实验学时数: 3 实验类 型 验证 日期20 12 年 3 月6 日星期二年级 学生姓名学号课任教师 1.实验目的 巩固如下概念:子类的继承性、子类对象的创建过程、成员变量的继承与隐藏、方法的继承与重写;掌握上转型对象的使用;掌握接口回调技术。 2.实验要求 实验前,应事先熟悉相关知识点,拟出相应的实验操作步骤,明确实验目的和要求;实验过程中,服从实验指导教师安排,遵守实验室的各项规章制度,爱护实验仪器设备;实验操作完成后,认真书写实验报告,总结实验经验,分析实验过程中出现的问题。 3.实验内容 1、继承 编写一个Java应用程序,除主类外,该程序中还有4个类:People、ChinaPeople、AmericanPeople和BeijingPeople类。要求如下:People类有访问权限是protected的double 型成员变量:height和weight,以及public void speakHello()、public void averageHeight()、public void averageWeight()方法;ChinaPeople类是People的子类,新增了public void chinaGongfu()方法。要求ChinaPeople重写父类的public void speakHello()、public void averageHeight()、public void averageWeight()方法;AmericanPeople类是People的子类,新增public void americanBoxing()方法。要求AmericanPeople重写父类的public void speakHello()、public void averageHeight()、public void averageWeight()方法;BeijingPeople类是ChinaPeople的子类,新增public void beijingOpera()方法。要求BeijingPeople重写父类的public void speakHello()、public void averageHeight()、public void averageWeight()方法;请按模板要求,将【代码】替换为Java程序代码。 2、上转型对象 编写一个Java应用程序,要求有一个abstract类,类名为Employee。Employee的子类有YearWorker、MonthWorker和WeekWorker。YearWorker对象按年领取薪水,MonthWorker按月领取薪水,WeekWorker按周领取薪水。Employee类有一个abstract方法:public abstract double earnings(); 子类必须重写父类的earnings()方法,给出各自领取报酬的具体方式。有一个
继承和派生实验报告
实验目的与要求: 1.掌握类的继承与派生关系以及实验方法,理解类的层次结构。 2.掌握派生类构造函数初始化基类成员和对象成员的方法。 3.掌握内联函数和默认函数。 4.掌握赋值兼容原则,掌握派生类的复制构造函数和赋值运算符的定义。 实验过程及内容: 1.实践教程实验二十二P81范例:定义一个继承与派生关系的类体系,在 派生类中访问基类成员。 ①先定义一个点类,包含x,y坐标数据成员,显示函数和计算面积的函数成员; ②以点为基类派生一个圆类,增加表示半径的数据成员,重载显示和计算面积的函数; ③定义一个线段类,以两个点类对象作数据成员,定义显示、求面积及长度函数,线段类采用聚合方式,因为有两个端点,不能用派生。 编程测试所定义的类体系。 本实验教程中有源码,请自行运行,体会和熟悉继承与派生的基本概念及实现方法,掌握派生类构造函数初始化基类成员和对象成员的方法等。2. 实践教程P83编程:多层派生练习,由上题Point类和Circle类继续派生出Cylinder类。要求计算圆柱的底面积、侧面积、全面积和体积。 请编写所有完整的成员函数,并编写主函数进行验证。 数据处理 1. (1)
(2)j结果报错,原因是派生类中的成员函数不能访问基类中的私有成员。(3)在Line类中添加两个数据成员。
2. #include 注意派生类的构造函数中对基类数据成员的初始化方法(即 Circle(double xv,double yv,double vv):Point(xv,yv)), 以及构造函数中对对象成员的初始化方法(即 Line(double xv1,double yv1,double xv2,double yv2) : start(xv1,yv1),end(xv2,yv2){ } ) 【要求】 (1)建立工程,录入上述程序,改变数据实验之。 (2)修改Point 类的数据成员x ,y 的访问权限为private ,再运行,结果如何? (3)如果不将Line 类设为 Point 类的友元,应采取什么措施?为哪个类增加数据或函数成员? 2.编程:多层派生练习,由上题Point 类和Circle 类继续派生出Cylinder 类。要求计算圆柱体的底面积、侧面积、全面积和体积。 实验四类的继承与多态 一、实验目的 1.掌握构造方法和成员方法重载的应用。 2.理解类的继承性的作用 3.领会面向对象编程的多态性。 二、实验内容与要求 基本概念 1.进一步理解继承的含义 新类可从现有的类中产生,并保留现有类的成员变量和方法并可根据需要对它们加以修改。新类还可添加新的变量和方法。这种现象就称为类的继承。 当建立一个新类时,不必写出全部成员变量和成员方法。只要简单地声明这个类是从一个已定义的类继承下来的,就可以引用被继承类的全部成员。被继承的类称为父类或超类(superclass),这个新类称为子类。 Java 提供了一个庞大的类库让开发人员继承和使用。设计这些类是出于公用的目的,因此,很少 有某个类恰恰满足你的需要。你必须设计自己的能处理实际问题的类,如果你设计的这个类仅仅实现了继承,则和父类毫无两样。所以,通常要对子类进行扩展,即添加新的属性和方法。这使得子类要比父类大,但更具特殊性,代表着一组更具体的对象。继承的意义就在于此。 2.了解成员变量的隐藏方式 所谓隐藏是指子类重新定义了父类中的同名变量,在子类L ine 中重新定义了x为x1,y 为y1,隐藏了父类Point 中的两个成员变量x 和y。子类执行自己的方法时,操作的是子类的变量,子类执行父类的方法时,操作的是父类的变量。在子类中要特别注意成员变量的命名,防止无意中隐藏了父类的关键成员变量,这有可能给程序带来麻烦。 3.了解成员方法的覆盖方式 (1)方法覆盖的定义与作用通过继承子类可以继承父类中所有可以被子类访问的成员方法,但如果子类的方法与父类方法同名,则不能继承,此时称子类的方法覆盖了父类的方法,简称为方法覆盖(override)。方法覆盖为子类提供了修改父类成员方法的能力。 4.理解类的多态性类的继承发生在多个类之间,而类的多态只发生在同一个类上。在一个类中,可以定义多个同名的方法,只要确定它们的参数个数和类型不同。这种现象称为类的多态。多态使程序简洁,为程序员带来很大便利。在O OP 中,当程序要实现多个相近的功能时,就给相应的方法起一个共同的名字,用不同的参数代表不同的功能。这样,在使用方法时不论传递什么参数,只要能被程序识别就可以得到确定的结果。 类的多态性体现在方法的重载(overload)上,包括成员方法和构造方法的重载。 实践应用 《面向对象程序设计》实验报告 实验六、类的继承 课程代码:1H10500学分:6 实验类型:实践型实验学时:4个学时 实验时间:2017.11.23 实验地点:5407 班级:计算机164 专业:计算机科学与技术专业学号:2016011147 姓名:林文辉 一、实验目的 (1)学习JAVA继承的思想 (2)了解JAVA语言继承的实现 二、实验内容及步骤 内容:在圆柱体类Cylinder里,用一个构造方法调用另一个构造方法 步骤:方案的设计与分析(类结构图。。。。。。) (1)找出对象的共同属性和行为 (2)设计代表共同状态与行为的类 (3)决定子类是否需要让某项行为有特定的不同的运行方式 (4)寻找更多抽象化的机会 (5)完成类的继承层次 三、实验过程及结果 实验1代码: abstract class Graphics { abstract void parameter(); abstract void area(); } class Rectangle extends Graphics{ double h,w; Rectangle(double u,double v){h=u;w=v;} void parameter() { S ystem.out.println("矩形高度为:"+h+",矩形宽度为:"+w); } void area() { S ystem.out.println("矩形面积为:"+(h*w)); } } class Circle extends Graphics{ double r; String c; Circle(double u,String v){r=u;c=v;} void parameter() { System.out.println("圆半径为:"+r+",圆颜色为:"+c); } void area() { System.out.println("圆面积为:"+(Math.PI*r*r)); } } class ExamAbs{ public static void main(String args[]) { Rectangle rec=new Rectangle(2.0, 3.0); Circle cir=new Circle(4.0, "Red"); Graphics[]g= {rec,cir}; for(int i=0;i 实验十类继承与接口(二) 课程名称:面向对象程序设计 实验项目名称:实验十类继承与接口(二) 学生姓名:专业:学号: 实验地点:实验日期:年月 【实验目的】 1.掌握Java类的继承特点,包括子类声明、对象创建 2. 掌握域的继承与隐藏的特点和用法 3. 掌握方法的继承与重写的特点和用法 【实验内容】 一. 改错题 检查下面代码是否有错,如果有错,写明错误原因,并修正错误。 (1) class Aaa{ int i; Aaa(){i=-1; } Aaa(int a){ i = a;} Aaa(double d){Aaa((int)d);} } 是否出错:是 出错原因(如果出错):构造器调用构造器不能直接用名 class Aaa{ int i; Aaa(){i=-1; } Aaa(int a){ i = a;} Aaa(double d){this((int)d);} } (2) class Aaa{ int i; Aaa(){i=-1; } Aaa(int a){ i = a;} Aaa(double a){int b=(int)a;this(b);} } 是否出错:是 出错原因(如果出错):this放第一行 class Aaa{ int i; Aaa(){i=-1; } Aaa(int a){ i = a;} Aaa(double a){this((int)a);} } (3) class Aaa{ int i; Aaa(int a){ i = a;} } class Bbb extends Aaa{ Bbb(){ } } 是否出错:是 出错原因(如果出错):父类没有无参构造,子类中药通过super调用父类构造class Aaa{ int i; Aaa(int a){ i = a;} } class Bbb extends Aaa{ Bbb(int a) { super(a); } } (4) class Aaa{ private int i;实验04 类的继承与多态
JAVA类的继承实验报告
面向对象--类继承与接口实验报告
实验6-继承与派生(1)