public class pratice {
private String man;
private int sco;
private int hour;
void setman(String n){
man=n;
}
void sethour(int h){
hour=h;
}
/**
*传值的方法
*/
//普通方法 格式:public 返回值类型 方法名(参数){}
//构造方法 格式:public 类名(参数 参数名){}
//每个类都有一个默认的无参的构造器(构造方法)
//1.创建对象 2.创建对象就给对象赋初值
public pratice(String m,int s){
man = m;
sco = s;
}
public void work(){
System.out.println(man+" has been working for "+hour);
}
public void money(){
sco+=15*hour;
System.out.println(man+" should get "+sco+" money ");
}
}
public class element extends pratice{
public element(String m, int s) {
super(m, s);
}
public void work(){
System.out.println(" should get money");
}
public static void main(String[] args) {
pratice p=new pratice("tom",10);
element h=new element("John",20);
h.sethour(10);
h.setman("tom");
method m=new method();
m.cut(h);
p.work();
h.money();
}
}
public class method {
public void cut(element p){
p.work();
}
}
感悟:类的继承就像是在生物学中,鼠是一个大类,也就是父类。而在鼠类中,有田鼠、家鼠等种类,每一种鼠类都有自己的生活方式,这就是方法的重写,与此同时,鼠类都有老鼠的一些特征,这就是父类中的一些属性的继承。
private String man;
private int sco;
private int hour;
void setman(String n){
man=n;
}
void sethour(int h){
hour=h;
}
/**
*传值的方法
*/
//普通方法 格式:public 返回值类型 方法名(参数){}
//构造方法 格式:public 类名(参数 参数名){}
//每个类都有一个默认的无参的构造器(构造方法)
//1.创建对象 2.创建对象就给对象赋初值
public pratice(String m,int s){
man = m;
sco = s;
}
public void work(){
System.out.println(man+" has been working for "+hour);
}
public void money(){
sco+=15*hour;
System.out.println(man+" should get "+sco+" money ");
}
}
public class element extends pratice{
public element(String m, int s) {
super(m, s);
}
public void work(){
System.out.println(" should get money");
}
public static void main(String[] args) {
pratice p=new pratice("tom",10);
element h=new element("John",20);
h.sethour(10);
h.setman("tom");
method m=new method();
m.cut(h);
p.work();
h.money();
}
}
public class method {
public void cut(element p){
p.work();
}
}
感悟:类的继承就像是在生物学中,鼠是一个大类,也就是父类。而在鼠类中,有田鼠、家鼠等种类,每一种鼠类都有自己的生活方式,这就是方法的重写,与此同时,鼠类都有老鼠的一些特征,这就是父类中的一些属性的继承。