(1)建立person类,包含private成员,name,sex,age,成员变量。
(2)建立person得构造函数
(3)建立一个显示过程show(),显示该对象的数据。
(4)派生一个学生类student,增加班级class,major,设计这些类得构造函数。
(5)建立class,major,对应的属性函数Sclass(),SMajor()。
(6)建立显示成员函数Show(),显示该学生对象说有成员数据。
class People {
private static final String Number = null;
//属性
String name;
String sex;
int age;
static long ID=120113000000000000L;
String address;
int peopleNumber;
static int COUNT=0;
//构造方法重载
People(){
this("Tom","nan",20,ID,"天津市滨海新区");
ID=120113000000000000L;
COUNT++;
}
People(long ID){
this("Tom","nan",20,ID,"天津市滨海新区");
COUNT++;
}
People(String name ,String sex ,int age , long ID, String address){
this.name=name;
this.sex =sex;
this.age =age;
People.ID = ID;
this.address =address;
COUNT++;
}
static void informations(int age,String address,long ID){
age=0;
}
void setSex(String sex){
this.sex=sex;
}
String getSex(){
return sex;
}
void setAge(int age){
if(age>=0 && age<=150){
this.age=age;
}
else{
System.out.println("数据不合理!");
}
}
int getAge(){
return this.age;
}
void setID(long ID){
if(Long.toString(ID).length()==18){
People.ID=ID;
}
else{
System.out.println("Number is long!");
}
}
long getID(){
return People.ID;
}
void setAddress(String newaddress){
address=newaddress;
}
String getAddress(){
return address;
}
public String information(){
return "name="+ this.name+",sex=" + this.sex +",address="+this.address+",ID="+People.ID+",age:"+this.age;
}
public void family(People []f){
for(int i=0; i<f.length;i++){
System.out.println(f[i].information());
}
}
}
import java.util.Scanner;
public class PeopleTest {
public static void main(String[] args) {
People stu =new People();
People stu2 = new People(102156789456325482L);
People stu3 = new People();
People stu4= new People("Tom","nan",20,1201332054698520142L,"天津市滨海新区");
stu.name="王明旭";
//stu.setAge(23);
stu.setID(102156789456325482L);
stu.setAddress("天津市滨海新区");
stu.setSex("女");
System.out.println(stu.name+"\tage:"+stu.getAge()+"\taddress:"+stu.getAddress()+"\t"+stu.getSex()+"\tnumber:"+stu.getID());
//stu.information();
stu2.setAge(-10);
stu2.name ="wangmingxu";
System.out.print("age:"+stu2.getAge()+"\t"+stu2.name+"\n");
//stu2.setID(123460);
//System.out.println(stu2.getID());
People[] stu4_family=new People[2];//数组
System.out.println("stu4_family:");
stu4_family[0]=stu3;
stu4_family[1]=new People("Mary","男",12,1201332054698520142L,"天津市滨海新区");
stu4.family(stu4_family);