一、类
1、person类
package com.wyq.study;
//书写父类的类
public class Pers {
//书写属性
private String name;
private int age;
private char sex;
//书写共有的取值赋值方法
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setAge(int age){
if(age<0||age>100){
System.out.println("年龄输入有误。。。");
this.age = 18;
}else
this.age = age;
}
public int getAge(){
return age;
}
public void setSex(char sex){
if(sex!= '男' && sex != '女'){
System.out.println("性别赋值有误。。。");
this.sex = '妖';
}else
this.sex = sex;
}
public char getSex(){
return sex;
}
//提供无参构造
public Pers(){
}
//提供有参构造
public Pers(String name,int age,char sex){
this.name = name;
this.setAge(age);
this.setSex(sex);
System.out.println("这里是有参构造。。。参数分别是:"+this.getName()+"\t"+this.getAge()+"\t"+this.getSex());
}
//提供普通方法
public void speak(String language,int time){
System.out.println(name+"正在讲"+language+"语言演讲,时间是:"+time+"点");
}
}
2、teacher类
package com.wyq.study;
public class Teacher {//书写类
//书写属性
private String name;
private int teachAge;
private char sex;
private String course;
//书写共有的取值赋值方法
//name
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
//teachAge
public void setTeachAge(int teachAge){
if(teachAge<0||teachAge>100){
System.out.println("您输入的年龄有误。。。");
this.teachAge = 1;
}else{
System.out.println("*****************");
this.teachAge = teachAge;
}
}
public int getTeachAge(){
return teachAge;
}
//sex
public void setSex(char sex){
if('男'!=sex||'女'!=sex){
System.out.println("性别输入有误");
this.sex ='人';
}else
this.sex = sex;
}
public char getSex(){
return sex;
}
//course
public void setCourse(String course){
System.out.println("程序调用了course方法");
this.course = course;
}
public String getCourse(){
System.out.println("程序调用course方法完成了。。。");
return course;
}
//提供无参构造
public Teacher(){
System.out.println("这里是无参构造");
}
//有参构造
public Teacher(String name,int teachAge,char sex,String course){
this.name = name;
this.setTeachAge(teachAge);
this.setSex(sex);
this.course = course;
System.out.println("这里是有参构造"+this.name+"\t"+this.getTeachAge()+"\t"+this.getSex()+"\t"+this.course);
}
}
3、student类
package com.wyq.study;
//书写类
public class Studentt {
//书写属性,属性私有化
private String name;
private int age;
private char sex;
private char grade;
//书写共有的取值赋值方法
//name
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
//age
public void setAge(int age){
//在赋值方法中对值进行合法性验证
if(age<0||age>100){
System.out.println("年龄有误。。。");
this.age = 18;
}else
this.age = age;
}
public int getAge(){
return age;
}
//sex
public void setSex(char sex){
if(sex!='男'||sex!='女'){
System.out.println("年龄输入有误。。。");
this.sex = '妖';
}else
this.sex = sex;
}
public char getSex(){
return sex;
}
//grade
public void setGrade(char grade){
this.grade = grade;
}
public char getGrade(){
return grade;
}
//无参构造
public Studentt(){
System.out.println("这里是无参构造。。。");
}
public Studentt(String name,int age,char sex,char grade){
this.setName(name);;
this.setSex(sex);
this.setAge(age);
this.setGrade(grade);;
System.out.println("这里是有参构造"+this.name+"\t"+this.getAge()+"\t"+this.getSex()+"\t"+this.grade);
}
}
二、测试
package com.wyq.study;
public class TestTeacher {
public static void main(String[] args) {
//创建对象
Teacher t = new Teacher("张三",-20,'怪',"计算机");
//调用teacher类中的成员方法
System.out.println("输出的结果为:"+t.getName()+"\t"+t.getTeachAge()+"\t"+t.getSex()+"\t"+t.getCourse());
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
Studentt st = new Studentt();
Studentt stt = new Studentt("李四",999,'狗','5');
System.out.println("输出的结果为:"+stt.getName()+"\t"+stt.getAge()+"\t"+stt.getSex()+"\t"+stt.getGrade());
//测试人员
System.out.println("****************");
Pers pe = new Pers();
Pers pe1 = new Pers("马六",102,'中');
pe1.speak("韩语", 3);
pe.speak("英语", 16);
}
}
三、总结
1、先写类
2、再写属性
3、再写共有的取值赋值方法:
在公有的取值赋值方法中对值进行合法性验证
4、再写无参构造
5、再写带参构造,在带参构造中对属性进行初始化
6、再写普通方法
7、再进行创建对象,同时调用构造方法,对属性进行了初始化
8、调用方法