定义一个teacher类,包括名字,年龄,学历,职位与自我介绍的方法
如图所示:
运行的结果
对于一些对象的值是有规定的就可以使用private私有再对其进行封装,比如性别只有男与女,就会使用到private与封装
如下
https://blog.csdn.net/qq_44973159/article/details/90340699
代码如下
package teacher;
public class teacher {
public String name;
public int age;
public String edcation;
public String position;
public teacher(String name, int age, String edcation, String position) {
this.age = age;
this.name =name;
this.edcation =edcation;
this.position=position;
}
public String introduction() {
return "大家好!,我是"+name+",我今年"+age+"岁了,我的学历是"+edcation+",我的职位是"+position;
}
static class test {
public static void main(String[] args) {
teacher th =new teacher ("李华",41,"本科","");
System.out.println(th.introduction());
}
}
}