定义一个学生类(Student)及它的属性和方法;定义主类(Example1)和main(),在main()方法中创建学生对象,并通过对象调用它的方法setName(),getName(),setAge(),getAge(),setSpecialty(),getSpecialty(),输出学生的名字name, age, specialty。
(代码)
package sh;
public class student {
private String name;
public int age;
public String specialty;
public void Apply(){
System.out.println("名字:"+name);
System.out.println("年龄: "+ age);
System.out.println("专业: "+specialty);
}
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 String getSpecialty() {
return specialty;
}
public void setSpecialty(String specialty) {
this.specialty = specialty;
}
}
package sh;
public class ro {
public static void main(String[] args) {
student student = new student();
student.setName("JIUJIUROU");
student.setAge(20);
student.setSpecialty("zi");
student.Apply();
}
}