实验6 继承与接口
Java语言程序设计实验六 类的继承(接口与包)实验报告 (2)

**大学**学院Java语言程序设计实验报告System.out.println("sum="+m1.num1+"+"+m1.num2+"+"+m1.num3+"="+m1.sum);}}问题:a、子类的sum,num1,num2分别是多少,父类的sum,num1,num2 分别是多少,如何在main中输出父类的sum,num1,num2子类:sum=600,num1=100,num2=200父类:sum=0,num1=0,num2=0;b、就这个例子,谈谈你对覆盖的理解?当子类的变量与父类的变量相同时,父类的变量被隐藏,main函数自动调用子类的变量2.运行下面的程序,理解方法的继承。
class Sort3{double max1,max2,max3;Sort3(){max1=-1;max2=-1;max3=-1;}double s;if (max1<max2){ s=max1;max1=max2;max2=s; }if (max1<max3){ s=max1;max1=max3;max3=s; }if (max2<max3){ s=max2;max2=max3;max3=s; }}}class sub_Sort3 extends Sort3{void subsort(double i,double j,double k){max1=i;max2=j;max3=k;sort(); //调用父类中的方法sort() …….① } }public class Class1{public static void main (String args[]){sub_Sort3 m1 = new sub_Sort3();m1.subsort(100,200,300);System.out.println("三个数从大到小为:"+m1.max1+","+m1.max2+","+m1.max3);}}问题:a、为什么标有.①处可以直接使用,而不需要生成Sort3的对象,然后使用该对象. sort()调用因为sub_sort3继承了sort,所以sort的方法就像是在子类中直接定义一样,可以被子类直接调用 B、程序实现什么功能?三个数之间互相比较大小3.运行下面的程序,理解方法的重载。
java实验报告——继承与接口

System.out.print('\t'+"平均分");
System.out.println('\t'+"成绩等级");
for(int i=0; i<pg.length;i++) {
System.out.print(pg[i].Name); System.out.print('\t'+pg[i].Stu_type); System.out.print('\t'+String.valueOf(pg[i].C_score)); System.out.print('\t'+String.valueOf(pg[i].English_score)); System.out.print('\t'+String.valueOf(pg[i].Java_score)); System.out.print('\t'+String.valueOf(pg[i].score)); System.out.println('\t'+pg[i].sco_Level); } System.out.println(); } }
String Name=""; String Stu_type=""; int C_score; int English_score; int Java_score; int score; // String sco_Level=""; Sco_Level sco_Level;
public Student(String name,String stu_type,int sco1,int sco2,int sco3) {
继承与接口实验

}
public void chinaGongfu() {
System.out.println("坐如钟,站如松,睡如弓");
}
}
class AmericanPeople extends People {
public void speakHello(){
BeijingPeople是ChinaPelple的子类,新增public void beijingOpera()方法。要求ChinaPeople重写父类的public void speakHello()、public void averageHeight()和public void averageWeight()方法。
System.out.println("American Average weight: 80.23 kg");
}
public void americanBoxing() {
System.out.println("直拳钩拳");
}
}
class BeijingPeople extends ChinaPeople {
System.out.println("How do you do");
}
public void averageHeight(){
System.out.println("American Average height: 188.0 cm");
}
public void averageWeight(){
public void speakHello(){
实验6 继承与接口

实验报告姓名学号专业班级课程名称 Java实验实验日期成绩指导教师批改日期实验名称实验 6 继承与接口[实验目的]1、掌握java 继承中父类及其子类的定义方法。
2、掌握子类重写父类同名方法的方法。
3、掌握接口的用法。
(1) 学习如何定义接口 ;(2) 掌握接口的实现方式 ;(3) 使用实现了接口的类 ;(4) 理解接口与抽象类的区别。
[实验要求]1、复习理论教学中所学的内容。
2、认真进行实验预习,查阅参考书,书写源程序,书写实验预习报告。
3、认真总结实验并书写实验报告。
[实验内容]1、类的继承性练习(1) 程序源代码如下。
public class Student{protected String xm; //姓名,具有保护修饰符的成员变量protected int xh;//学号void setdata(String xm,int xh) //设置数据的方法{this.xm=xm;this.xh=xh;}public void print() //输出数据的方法{System.out.println(xm+", "+xh);}}import java.util.*;public class Exe_1 extends Student{public Exe_1(){};public static void main(String[] args) {Scanner input=new Scanner(System.in);Exe_1 text=new Exe_1();System.out.println("请输入姓名和学号:");text.setdata(input.next(), input.nextInt());System.out.println("显示如下:");text.print();}}(2) 编译源并运行程序。
贴图如下(二)创建将被继承的类(1) 程序功能:通过Student类产生子类CollegeStudent,其不仅具有父类的成员变量xm(姓名)、xh(学号),还定义了新成员变量xy(学院)、bj(bj)。
java实验报告——继承与接口

南京理工大学泰州科技学院实验报告书课程名称:《Java 面向对象程序设计》实验题目:实验四继承与接口班级:09计算机(2)学号:0909030218姓名:尤源指导教师:李丛一、实验目的1.掌握Java语言的类的继承的概念。
2.掌握Java语言中抽象类的使用。
3.掌握Java语言中接口的使用4.掌握eclipse集成开发环境的使用。
二、实验内容1.类的继承,具体要求如下:(1)定义一Person类,该类具有属性人名、年龄、身份证号等信息以及将属性信息作为字符串返回的一方法;(2)定义一Student类,让该类继承Person类,该类除了具有属性人名、年龄、身份证号等信息以外还有学号,所在学校等信息;该类也具有将属性信息作为字符串返回的一方法;(3)编写测试类,测试这两个类2.定义一个动物抽象类Animal,该类有一个抽象的方法cry();定义一个小猫类Cat,该类继承了Animal类并实现了cry()方法,当调用cry()方法时打印“小猫喵喵叫”,定义一个小狗类Dog,该类也继承了Animal类并实现了cry()方法,当调用cry()方法时打印“小狗汪汪叫”。
3. 接口的运用。
定义一接口接口名叫Usb,该接口声明了两个方法分别为start()和stop()方法,定义一U 盘类UsbDiskWriter,一照相机类Camera、一手机类Mobile,让它们都实现该接口。
三、实验步骤实验(1)编写代码实验(2)编写代码实验(3)编写代码四、实验结果实验(1)运行结果实验(2)运行结果实验(3)运行结果五、结果分析1. 子类若想调用父类的构造函数必须要用super关键字。
2.接口体中只能运用抽象类。
3.在同一个java文件中只能在入口函数的类中用public。
实验6继承与接口

山西大学计算机与信息技术学院实验报告(2)编译源并运行程序。
贴图如下(三) 了解成员方法的覆盖方式⑴编写覆盖了 Object 类toString 方法的一个类,并用 System.out.println()输出该类的一个对象。
String ToString(){super .toString()+"\n"+str; }public static void main(String[] args) {OverWriteToStri ng o = new OverWriteToStri ng("This is a method "+"to overwrite ToStri ng method!");System. out .println(o.ToString()); } (2)试着以Point 类为例,尝试为Object 类的cione()和equals ()方法进行覆盖,Point类包含程序代码:public class OverWriteToString { private public } public thisString str; OverWriteToStri ng(){OverWriteToStri ng(Stri ng str){ .str = str;publicreturn 图二}运行结果贴图图三私有成员x,y,构造方法1 (包含两个参数a,b),构造方法2 (参数为Point p), clone方法,equals方法,toString方法。
用TestPoint类进行测试。
程序代码:public class Point {private int x; private int y; public Poin t(){ }public Point( int a, int b){x = a;y = b;}public Poi nt(Poi nt p){x = p.x;y = p.y;}//重写equals()方法public boolean equals(Object o){if (o instanceof Point){ retur n (x==((Poi nt)o).x && y==((Poi nt)o).y);}elsereturn false ;}//重写toString ()方法public Stri ng toStri ng(){return super .toString()+"\n 该点的坐标为("+x+","+y+")";}//重写cione()方法public Object cione() throws CioneNotSupportedException { return new Point( this );}}class TestPoint{public static void main(String[] args) throws CioneNotSupportedException { Poi nt p = new Poi nt(2,3);Point p1= new Poin t(p);Poi nt p2 = (Poi nt)p.clo ne();System. out .println("p 与pl 相等吗?"+p.equals(p1));System. out .println("p 与p2相等吗?"+p.equals(p2));System. out .println(p);System. out .println(p1);System. out .println(p2);}运行结果贴图}}public Stri ng toStri ng(){return super .toString()+"\n 该线起点为:("+p1.x+","+p1.y+")"+"终点为:("+p2.x+","+p2.y+")";}}class TestLine{public static void main(String[] args) {Point p1 = new Poi nt();Poi nt p2 = new Poi nt(3,4);Line l = n ewL in e(p1,p2);System. out.println(”线l 的长度为:"+l.legth());System. out .println(l);}}运行结果贴图:图五(五)接口的实现与运用实验任务:本实验的任务是设计和实现一个Soundable接口,该接口具有发声功能,同时还能够调节声音大小。
继承与接口实验

继承与接口实验实验1class People{protected double weight,height;public void speakHello(){System.out.println("yayawawa");}public void averageHeight(){height=173;System.out.println("average height:"+height);}public void averageWeight(){weight=70;System.out.println("average weight:"+weight);}}class ChinaPeople extends People{【代码1】 //重写public void speakHello()方法,要求输出类似“你好,吃了吗”这样的 //汉语信息【代码2】 //重写public void averageHeight()方法,要求输出类似//“中国人的平均身高:168.78厘米”这样的汉语信息【代码3】 //重写public void averageWeight()方法,//要求输出类似“中国人的平均体重:65公斤”这样的汉语信息public void chinaGongfu(){【代码4】//输出中国武术的信息,例如:"坐如钟,站如松,睡如弓"等}}class AmericanPeople extends People{【代码5】 //重写public void speakHello()方法,要求输出类似//“How do you do”这样的英语信息。
【代码6】 //重写public void averageHeight()方法【代码7】 //重写public void averageWeight()方法public void americanBoxing(){【代码8】//输出拳击的信息,例如,“直拳”、“钩拳”等}}class BeijingPeople extends ChinaPeople{【代码9】 //重写public void speakHello()方法,要求输出类似“您好”这样的汉语信息【代码10】 //重写public void averageHeight()方法【代码11】 //重写public void averageWeight()方法public void beijingOpera(){【代码12】//输出京剧的信息}}public class Example{public static void main(String args[]){ChinaPeople chinaPeople=new ChinaPeople();AmericanPeople americanPeople=new AmericanPeople();BeijingPeople beijingPeople=new BeijingPeople();chinaPeople.speakHello();americanPeople.speakHello();beijingPeople.speakHello();chinaPeople.averageHeight();americanPeople.averageHeight();beijingPeople.averageHeight();chinaPeople.averageWeight();americanPeople.averageWeight();beijingPeople.averageWeight();chinaPeople.chinaGongfu();americanPeople.americanBoxing();beijingPeople.beijingOpera() ;beijingPeople.chinaGongfu();}}实验2abstract class Employee{public abstract double earnings();}class YearWorker extends Employee{【代码1】//重写earnings()方法}class MonthWorker extends Employee{【代码2】//重写earnings()方法。
实验6 继承与接口

山西大学计算机与信息技术学院}(2) 编译源并运行程序。
贴图如下图一(二)创建将被继承的类(1) 程序功能:通过Student类产生子类CollegeStudent,其不仅具有父类的成员变量xm(姓名)、xh(学号),还定义了新成员变量xy(学院)、bj(bj)。
在程序中调用了父类的print 方法,同时可以看出子类也具有该方法。
程序代码:public class CollegeStudent extends Student{protected String xy;protected int bj;void setdata(String xm,int xh,String xy,int bj){super.setdata(xm, xh);this.xy = xy;this.bj = bj;}public void print() {super.print();System.out.print("学院:"+xy+"班级:"+bj);}}class TestCollegeStudent{public static void main(String[] args) {CollegeStudent cs = new CollegeStudent();cs.setdata("小蓝", 2010242555, "计算机学院", 1);cs.print();}}运行结果贴图:图二(三)了解成员方法的覆盖方式(1)编写覆盖了Object 类toString方法的一个类,并用System.out.println()输出该类的一个对象。
程序代码:public class OverWriteToString {private String str;public OverWriteToString(){}public OverWriteToString(String str){this.str = str;}public String ToString(){return super.toString()+"\n"+str;}public static void main(String[] args) {OverWriteToString o = new OverWriteToString("This is a method "+"to overwrite ToString method!");System.out.println(o.ToString());}}运行结果贴图:图三(2)试着以Point类为例,尝试为Object类的clone()和equals()方法进行覆盖,Point类包含图四(四)this、super和super()的使用(1)程序功能:说明this、super 和super()的用法。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
山西大学计算机与信息技术学院
public class LX6_1 extends Student{
public LX6_1(){};
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
LX6_1 text=new LX6_1();
System.out.println("请输入姓名和学号:");
text.setdata(input.next(), input.nextInt());
System.out.println("显示如下:");
text.print();
}
}
(2) 编译源并运行程序。
贴图如下
(二)创建将被继承的类
(1) 程序功能:通过Student类产生子类CollegeStudent,其不仅具有父类的成员变量xm(姓名)、
xh(学号),还定义了新成员变量xy(学院)、bj(班级)。
在程序中调用了父类的print 方法,同时可以看出子类也具有该方法。
程序代码:
package T6;
public class CollegeStudent extends Student {
private String xy;
private String bj;
public CollegeStudent() {
};
public void setdata(String xm, int xh, String xy, String bj) { super.setdata(xm, xh);
this.xy = xy;
this.bj = bj;
}
public void print() {
super.print();
System.out.println(xy + ", " + bj);
}
}
package T6;
import java.util.*;
public class LX6_2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
CollegeStudent new1 = new CollegeStudent();
System.out.println("请输入姓名、学号、学院、班级:");
new1.setdata(input.next(), input.nextInt(), input.next(), input.next());
System.out.println("显示如下:");
new1.print();
}
}
运行结果贴图:
(三)了解成员方法的覆盖方式
(2)试着以Point类为例,尝试为Object类的clone()和equals()方法进行覆盖,Point类包含私有成员x,y,构造方法1(包含两个参数a,b),构造方法2(参数为Point p),clone方法,equals 方法,toString方法。
用TestPoint类进行测试。
程序代码:
package T6;
public class Point {
private double x;
private double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public Point(Point p) {
this.x = p.x;
this.y = p.y;
}
protected Object clone() {
Object a = new Point(x, y);
return a;
}
public boolean equals(Object o) { // 一定是object
if (o instanceof Point) {
if (x == ((Point) o).x && y == ((Point) o).y) // 转换,且转换后的真题加括号
return true;
else
return false;
} else
return false;
}
public String toString() {
System.out.println(super.toString());
return ("x=" + x + " y=" + y);
}
}
package T6;
public class TestPoint {
public static void main(String[] args) {
Point a = new Point(3, 2);
Point d = new Point(3, 21);
Point b = new Point(a);
Object c = a.clone();
System.out.println(a.equals(d));
System.out.println(a.equals(c));
System.out.println("a " + a);
System.out.println("b " + b);
System.out.println("c "+ c);// 因为clone定义是new的为Point,所以为Point 类
}
}
运行结果贴图:
(四)this、super和super()的使用
length = Math.sqrt((x2 - super.x) * (x2 - super.x) + (y2 - super.y) * (y2 - super.y));
return length;
}
public String toString() {
return"起点坐标:(" + super.x + "," + super.y + ")终点坐标为:(" + x2 + ","
+ y2 + ") 长度为:" + length();
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入起点坐标");
Line line1 = new Line(input.nextDouble(), input.nextDouble());
System.out.println("请输入终点坐标");
line1.setLine(input.nextDouble(), input.nextDouble());
System.out.println(line1);
}
}
运行结果贴图:
(五)接口的实现与运用
实验任务 :
本实验的任务是设计和实现一个 Soundable 接口 , 该接口具有发声功能 , 同时还能够调节声音大小。
Soundable 接口的这些功能将会由 3 种声音设备来具体实现 , 它们分别是收音机Radio 、随身昕 Walkman 和手机 Mobilephone 。
最后还要设计一个应用程序类来使用这些实现了
运行结果贴图:
实验分析:接口的特点在于只定义能做什么 , 而不定义怎么去做。
在本实验中 , 收音机Radio, 随身听 Walkman 和手机 Mobilephone 分别以自己的方式实现了 Soundable 接口 , 当接口成为 Listen(Soundable s) 方法的形参时 , 任何实现了 Soundable 接口的对象都能成为它的实参 , 如果不用接口作形参 , 那就必须写 3 个不同的方法 , 即
listenRadio(Radio r),
listenWalkman(Walkman w) ,
listenMobilephone(Mobilephone m)。
课后作业:p321 11.1 11.3 11.5 11.7 11.9 11.13 11.15 11.18 11.22
P396 14.1 14.3 14.4 14.6 14.27
11.1:
答:A.运行后显示:A's no-arg constructor is invoked。