110/111private关键字定义学生类和fins关键字的作用
private关键字定义学生类练习
定义Student类
private String name;
private int age;
private boolean male;
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;
}
public boolean isMale() {
return male;
}
public void setMale(boolean male) {
this.male = male;
}
main方法输出即可
Studentt studentt = new Studentt(); studentt.setName("乌拉"); studentt.setAge(11); studentt.setMale(true); System.out.println("姓名"+studentt.getName()); System.out.println("年龄"+studentt.getAge()); System.out.println("是不是男的"+studentt.isMale());
fins关键字的作用
作用:
1、调用本类中的方法
2、表示类中的属性
3、可以使用this调用本类的构造方法
4、this表示当前对象
1、调用本类中的方法
public class ThisDemo {
public static void main(String[] args) {
DemoThis d=new DemoThis(20);
d.tellThis();
}
}
class DemoThis{
private int age;
public DemoThis(int a) {
age=a;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void tellThis() {
System.out.println("年龄为:"+this.getAge());
}
}
//结果:年龄为:20
使用“this.方法名称()”格式可以调用本类中的方法