Java面向对象编程(封装/继承/多态)实例解析-创新互联
本文主要介绍了面向对象的三大特征实例解析,下面看看具体内容。

封装
封装一个Teacher和Student类
package com.hz.test;
public class Teacher {
private String name;
private String majorDirection;
private String teachCourse;
private int teachAge;
public Teacher() {
super();
}
public Teacher(String name,String majorDirection,String teachCourse,int teachAge) {
this.name = name;
this.majorDirection = majorDirection;
this.teachCourse = teachCourse;
this.teachAge = teachAge;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMajorDirection() {
return majorDirection;
}
public void setMajorDirection(String majorDirection) {
this.majorDirection = majorDirection;
}
public String getTeachCourse() {
return teachCourse;
}
public void setTeachCourse(String teachCourse) {
this.teachCourse = teachCourse;
}
public int getTeachAge() {
return teachAge;
}
public void setTeachAge(int teachAge) {
this.teachAge = teachAge;
}
public String toString() {
return "姓名=" + getName() + ", 专业方向=" + getMajorDirection()
+ ", 所教课程=" + getTeachCourse() + ", 教龄=" + getTeachAge();
}
} 网站名称:Java面向对象编程(封装/继承/多态)实例解析-创新互联
URL链接:http://www.jxjierui.cn/article/egosd.html


咨询
建站咨询
