实验时间:2019.4.15
必做
1.1) 区别:隐式类型转换将类型为子类型得数据转换成为类型为其父类型的数据时,可以不需要强制类型转换运算符”( )”; 显式类型转换将类型为子类型得数据转换成为类型为其父类型的数据时,需要强制类型转换运算符”( )”。
1.2) 无法继承J_Employee;重新定义一个J_Employee。java文件
源文件:
package ex04;
public class J_Employee
{
public int m_workyear;
//工作年限
public J_Employee()
{
m_workyear = 1;
} //构造方法结束
} //类结束
package ex04;
public class J_Teacher extends J_Employee
{
public int m_classHour; //授课的课时
public J_Teacher()
{
m_classHour = 96;
} //构造方法结束
public void mb_printInfon()
{
System.out.println("该教师的工作年限为:"+ m_workyear);
System.out.println("该教师的授课课时为:"+ m_classHour);
}//方法体mb_printInfon结束
public static void main(String[] args)
{
J_Teacher tom = new J_Teacher();
tom.mb_printInfon();
} //方法结束
} //类结束
package ex04;
public class Student extends J_Employee {
public int studyYears; //学习年长
//工作年限
public Student()
{
studyYears = 15;
} //构造方法结束
public void mb_printInfon()
{
System.out.println("该学生的学习年限为:"+studyYears);
System.out.println("该学生工作年限为:"+m_workyear);
}
public static void main(String[] args) {
Student zhangsan = new Student();
zhangsan.mb_printInfon();
}
}
运行结果:
J_Teacher.java
Student.java
源码:
Employee.java
package ex04.asia.china;
public class Employee {
//域变量为name和age;
public String name;
public int age;
}
Teacher.java
package ex04.asia.china.guangdong;
import ex04.asia.china.Employee;
public class Teacher extends Employee {
/*
*域变量为工作年限(workYears)和职称(jobTitle);成员方法包括构造方法与getter方法;
*/
private int workyears;
private String iobTitle;
public Teacher(int workyears,String iobTitle) {
this.workyears = workyears;
this.iobTitle = iobTitle;
}
public int getWorkyears() {
return workyears;
}
//public void setWorkyears(int workyears) {
//this.workyears = workyears;
//}
public String getIobTitle() {
return iobTitle;
}
//public void setIobTitle(String iobTitle) {
// this.iobTitle = iobTitle;
//}
}
TestTeacher.java
package ex04.asia.china.guangdong.zhuhai;
import ex04.asia.china.guangdong.Teacher;
public class TestTeacher {
public static void main(String[] args) {
Teacher jilinTeacher_zhangsan = new Teacher(20,"老师");
jilinTeacher_zhangsan.name = "jilin";
jilinTeacher_zhangsan.age = 30;
System.out.println("年龄:"+jilinTeacher_zhangsan.age);
System.out.println("姓名:"+jilinTeacher_zhangsan.name);
System.out.println("工作年限:"+jilinTeacher_zhangsan.getWorkyears());
System.out.println("职称:"+jilinTeacher_zhangsan.getIobTitle());
}
}
运行结果:
实验总结:
学习巩固了继承的使用方法,复习了封装getter方法的使用。