面向对象终于接近尾声了。
晚上写了三个案例,每次都因为构造方法要不停的写(无参构造、带全部参数构造)而烦。。。
每次继承一次都要写一次,每次实例化一个接口又要写一次。。
真心 好烦、、、、、
不过、、、还好 没在面向对象的部分落下。。
附代码:需求:实现基础班学生与老师,就业班学生与老师实例化,而且实现抽烟功能。
//定义抽烟接口
interface Smoking {
public abstract void smoking();
}
// 定义老师类 功能 教学 成员 name age
abstract class Teacher {
private String name;
private int age;
Teacher() {
}
public Teacher(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
abstract void teach();
}
// 定义抽象学生类 姓名 年龄 方法:学习
abstract class Student {
private String name;
private int age;
Student() {
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
abstract void study();
}
// 定义基础班老师类
class JavaSeTeacher extends Teacher {
JavaSeTeacher() {
}
public JavaSeTeacher(String name, int age) {
super(name, age);
}
public void show() {
System.out.println(getName() + "--" + getAge());
}
public void teach() {
System.out.println("基础班老师讲JAVASE课程");
}
}
// 定义就业班老师类
class JavaEeTeacher extends Teacher {
JavaEeTeacher() {
}
public JavaEeTeacher(String name, int age) {
super(name, age);
}
public void show() {
System.out.println(getName() + "--" + getAge());
}
public void teach() {
System.out.println("就业班老师讲JAVAEE课程");
}
}
// 定义基础班学生类
class JavaSeStudent extends Student {
JavaSeStudent() {
}
public JavaSeStudent(String name, int age) {
super(name, age);
}
public void show() {
System.out.println(getName() + "--" + getAge());
}
public void study() {
System.out.println("基础班学习学JavaSE");
}
}
// 定义就业班学生类
class JavaEeStudent extends Student {
JavaEeStudent() {
}
public JavaEeStudent(String name, int age) {
super(name, age);
}
public void show() {
System.out.println(getName() + "--" + getAge());
}
public void study() {
System.out.println("就业班学习学JavaEE");
}
}
// 定义抽烟类基础班老师
class JavaSeTeacherSmoking extends JavaSeTeacher implements Smoking {
JavaSeTeacherSmoking() {
}
public JavaSeTeacherSmoking(String name, int age) {
super(name, age);
}
public void smoking() {
System.out.println("基础班的老师在抽烟 " + "---" + "和天下");
}
}
// 定义学生类抽烟对象
class JavaEeStudentSmoking extends JavaEeStudent implements Smoking {
JavaEeStudentSmoking() {
}
public JavaEeStudentSmoking(String name, int age) {
super(name, age);
}
public void smoking() {
System.out.println("就业班的学生在抽烟 " + "---" + "1916");
}
}
public class ExtendsStudentTeacher {
public static void main(String[] args) {
// JavaEeStudentSmoking js = new JavaEeStudentSmoking("老三", 24);
JavaEeStudentSmoking js = new JavaEeStudentSmoking("老三", 24);
js.study();
js.show();
js.smoking();
JavaSeTeacherSmoking jt = new JavaSeTeacherSmoking("老四", 40);
jt.teach();
jt.show();
jt.smoking();
// JavaSeTeacherSmoking jt = new JavaSeTeacherSmoking("老四", 40);
// jt.smoking();
}
}
|