/*
一个简单java类,包含私有的属性,无参数的构造函数和其他有需要的重载构造函数,
公有的setter和getter,类定义中不调用输出函数
*/
class Student{
private String name ;
private int age ;
private String college ;
private String depart ;
public Student (){}
public Student (String name , int age , String college , String depart){
this.setName(name);
this.setAge(age);
this.setCollege(college);
this.setDepart(depart);
}
public void setName(String name){
this.name = name;
}
public void setAge(int age){
this.age = age ;
}
public void setCollege(String college){
this.college = college ;
}
public void setDepart(String depart){
this.depart = depart ;
}
public String getName(){
return this.name ;
}
public int getAge(){
return this.age ;
}
public String getCollege(){
return this.college ;
}
public String getDepart(){
return this.depart;
}
public String getInfo(){
return "学生信息如下:" + "\n" +
"姓名:" + this.getName() + "\n" +
"年龄:" + this.getAge() + "\n" +
"学校:" + this.getCollege() + "\n" +
"学院:" + this.getDepart();
}
}
public class test1{
public static void main (String args[]){
Student stu1 = new Student("张三" , 22 , "shu" , "computer science and techololege") ;
System.out.println(stu1.getInfo());
}
}
一个简单java类
最新推荐文章于 2024-02-23 18:20:45 发布