您的位置:首页 > 编程语言 > Java开发

Java第五次实验要求

2016-04-05 20:35 555 查看
实验五 继承与接口(一)
 
一、 实验目的
1、 掌握Java语言中继承的基本概念及使用方法;
2、 掌握Java语言中成员变量隐藏的基本概念及使用方法;
3、 掌握Java语言中super关键字的使用方法;
4、 理解继承与组合的区别;
5、 理解程序设计中代码复用的重要性。
 
二、 实验要求
1、 根据实验步骤中提出的要求,使用记事本编写相应的Java程序;
2、 使用JDK中提供的javac以及java命令编译、运行自己所编写的Java程序;
3、 根据编译与运行过程中所获得的错误信息修改程序直至获得正确的结果;
4、 记录实验中遇到的各类问题并以及解决办法。
 
三、 实验步骤
1、 父类与子类
(1) 编写一个基本的Point类,其代码如下:
class Point{
private double x;
private double y;

Point(double x, double y){
this.x=x;
this.y=y;
}
 
void print(){
System.out.printf("x=%4.2f,y=%4.2f\n",x,y);
}
}
 
(2) 编写Point类的一个子类ColorPoint,并在该类中实现主方法,其代码如下:
class ColorPoint extends Point{
int colorType;
final static char[] colors={'R','G','B'};
ColorPoint(double x,double y, int colorType){
super(x,y);
this.colorType=colorType;
}
void print(){
super.print();
System.out.printf("color:%s",colors[colorType]);
}
public static void main(String args[]){
ColorPoint cp=new ColorPoint(8,7,1);
cp.print();
}
}
 
(3) 提问:ColorPoint构造方法中的两条语句是否可以颠倒?为什么?
 
2、 父类成员变量的访问
(1) 已知有父类Person与子类Student,其代码如下:
class Person{
private String idNo;
String name;
boolean sex;
int age;

 
public Person(String idNo,String name,boolean sex,int age){
this.idNo=idNo;
this.name=name;
this.sex=sex;
this.age=age;
}
 
public void display(){
System.out.println("姓名:    "+name);
System.out.println("性别:    "+(sex?"男":"女"));
System.out.println("年龄:    "+age);
System.out.println("身份证:  "+idNo);
}
 
}
 
class Student extends Person{
String studentNo;
 
public Student(String studentNo,String idNo,String name,boolean sex,int age){
super(idNo,name,sex,age);
this.studentNo=studentNo;
}
 
public void display(){
System.out.printf("学号:  %s\n",studentNo);
super.display();
}
}
 
class PersonTest{
public static void main(String args[]){
Student st=new Student("10201198","36...","***",true,20);
st.display();
}
}
 
(2) 将代码编译并运行,考虑子类是如何访问父类的私有变量的?
(3) 如果Student在example.school包中,而Person和PersonTest在example包中,而且将Person的display方法改为:
public void display(){
System.out.printf("学号:  %s\n",studentNo);
System.out.printf("姓名:  %s**\n",name.substring (0,1));
}
(4) 那么其它的代码应该如何修改?
 
3、 父类成员变量的访问
(1) 已知有父类Person与子类Student,其代码如下:
class Father{
String name;
Father(String name){
this.name=name;
}
}
 
class Son extends Father{
String name;
public Son(String name,String fatherName){
……
}
public void display(){
……
}
public static void main(String args[]){
Son son=new Son("苏轼","苏洵");
son.display();
}
}
 
(2) 请将省略处的代码补充完整,使程序运行后显示结果“苏轼的父亲是苏洵”
 
4、 继承与组合
(1) 请分别采用继承的方式与组合的方式实现圆柱体,并比较两种方式的优劣。
(2) 已知有一帐号类,其代码如下:
class Account{
String username;
String password;
 
public Account(String username,String password){
this.username=username;
this.password=password;
}
 
public boolean login(String username,String password){
return username.equals(this.username) &&
password.equals(this.password);
}
}
 
(3) 目前已知每位教师只有一个账号,请采用继承的方式编写一个程序实现教师的登录。以下是主方法的代码:
public static void main(String args[]){
Teacher teacher=new Teacher("李明","1","1");
//(name, username, password)
Scanner reader=new Scanner(System.in);
System.out.println("请输入用户名:");
String username=reader.nextLine();
System.out.println("请输入密码:");
String password=reader.nextLine();
boolean flag=teacher.login(username,password);
System.out.println(teacher.name+"老师登录"+(flag?"成功!":"失败!"));
}
 
(4) 如果每位教师都有两个账号,请采用组合的方式编写一个程序实现教师的登录。以下是主方法的代码:
……
Account[] accounts = {new Account("1","1"),new Account("2","2")};
Teacher teacher=new Teacher("李明",accounts);
……
(将上述代码替换第三步中的第一行)
 
四、 实验结果
五、 实验小结
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: