对于super的学习
其中有着三个大类(Human,CNPersons,Students)
Human类代码如下
package super 的使用;
public class Human {
private String name;
private int age;
private String sex;
public Human ( String name, int age, String sex) {
super ( ) ;
this . name = name;
this . age = age;
this . sex = sex;
}
public Human ( ) { } ;
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 getSex ( ) {
return sex;
}
public void setSex ( String sex) {
this . sex = sex;
}
public void hu ( String name, int age, String sex) {
System. out. print ( "我的名字是" + name+ ",我今年" + age+ "岁,我是" + sex+ "生," ) ;
}
}
CNpersons类代码如下
package super 的使用;
public class CNPersons extends Human {
private String shuxiang;
int year;
public String getShuxiang ( ) {
return shuxiang;
}
public void setShuxiang ( String shuxiang) {
this . shuxiang = shuxiang;
}
public CNPersons ( ) { } ;
public CNPersons ( String name, int age, String sex, String shuxiang) {
super ( name, age, sex) ;
this . shuxiang = shuxiang;
if ( name. length ( ) < 8 && name. length ( ) > 0 ) {
System. out. println ( name) ;
}
else
System. out. println ( "输入不符合规则!" ) ;
}
public void shuxiang ( int year) {
switch ( year% 12 ) {
case 0 : shuxiang= "猴" ; break ;
case 1 : shuxiang= "鸡" ; break ;
case 2 : shuxiang= "狗" ; break ;
case 3 : shuxiang= "猪" ; break ;
case 4 : shuxiang= "鼠" ; break ;
case 5 : shuxiang= "牛" ; break ;
case 6 : shuxiang= "虎" ; break ;
case 7 : shuxiang= "兔" ; break ;
case 8 : shuxiang= "龙" ; break ;
case 9 : shuxiang= "蛇" ; break ;
case 10 : shuxiang= "马" ; break ;
case 11 : shuxiang= "羊" ; break ;
default : shuxiang= "输入错误" ; break ;
}
System. out. print ( "我属" + shuxiang+ "," ) ;
}
public void per ( int year) {
CNPersons cn = new CNPersons ( ) ;
cn. shuxiang ( year) ;
}
}
Students类代码如下
package super 的使用;
public class Students extends CNPersons {
public Students ( String name, int age, String sex, String shuxiang) {
super ( name, age, sex, shuxiang) ;
}
public Students ( ) { }
public void school ( int num, String schoolname, String grade) {
System. out. println ( "学号为" + num+ ",在" + schoolname+ "上" + grade+ "。" ) ;
}
public static void main ( String[ ] args) {
Students stu1 = new Students ( ) ;
stu1. hu ( "张三" , 21 , "男" ) ;
stu1. per ( 2000 ) ;
stu1. school ( 1 , "某某高校" , "大二" ) ;
}
}
运行代码如下