您的位置:首页 > 编程语言 > Java开发

java,类的继承

2016-03-07 16:42 579 查看
public class Father {
private String name;
private int age;
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 Father()
{
System.out.println("父类的构造方法");
}
//工作
public void work()
{
System.out.println("我劳动我光荣");
}

}


public class TestJiCheng {

public static void main(String[] args) {
Father f=new Father();
f.setName("父亲");
f.setAge(50);
System.out.println("名字是"+f.getName()+"年龄是"+f.getAge());
f.work();
Son s=new Son();
s.setName("儿子");
s.setAge(20);
System.out.println("名字是"+s.getName()+"年龄是"+s.getAge());
s.work();
s.sing();

}

}


public class TestJiCheng {

public static void main(String[] args) {
Father f=new Father();
f.setName("父亲");
f.setAge(50);
System.out.println("名字是"+f.getName()+"年龄是"+f.getAge());
f.work();
Son s=new Son();
s.setName("儿子");
s.setAge(20);
System.out.println("名字是"+s.getName()+"年龄是"+s.getAge());
s.work();
s.sing();

}

}


public class Son extends Father {
public Son()
{
System.out.println("子类的构造方法");
}

//Object a;所有类的父类
public void sing()
{
System.out.println("我喜欢唱歌");
}
//覆盖(重写),子类要同名同参数父类才能形成覆盖
public void work()
{
System.out.println("我不喜欢上班,我要去海选");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: