继承
Animal
/* hao
* 2018-2-14 11:39:22
* 继承
*/
public class Animal {
int age;
String name;
void eat(){
System.out.println("吃");
}
}
/* hao
* 2018-2-14 11:41:52
* Animal子类
*/
public class Dog extends Animal {
}
public class Initail {
public static void main(String[] args) {
// TODO Auto-generated method stub
Dog dog = new Dog();
dog.age = 10;
dog.name="xxx";
dog.eat();
}
}
User
/* hao
* 2018-2-13 16:42:24
* User类
*/
public class User extends Object{
String name;
int code;
String type;
//创建无参方法
User(){
System.out.println(“父对象出生了”);
}
User(String name,int code,String type){
this.name=name;
this.code=code;
this.type=type;
}
void speak(){
System.out.println("爱说");
}
void eat(){
System.out.println("爱吃");
}
void show(){
System.out.println("展示自己信息");
}
}
/* hao
* 2018-2-14 21:32:35
* 子类
*/
public class Worker extends User {
// Worker(){
// super();
// System.out.println(“子对象出生了”);
// }
Worker(String name, int code, String type) {
// super(); // 显式调用
super(name, code, type); // 显式调用
}
public static void main(String [] args){
Worker w = new Worker("aaa", 111, "222");
w.speak();
w.eat();
}
void speak(){
System.out.println("能说");
}
void eat(){
System.out.println("能吃");
}
void print(){
System.out.println("Worker [name=" + name + ", code=" + code + ", type=" + type + "]");
}
}
/* hao
* 2018-2-14 21:27:57
* 测试
*/
public class TestUser {
public static void main(String[] args) {
// TODO Auto-generated method stub
Worker wo = new Worker("www",111,"ooo");
Worker rk = new Worker("rrr",222,"kkk");
wo.print();
rk.print();
}
}
我宁愿失去一个印度,也不愿失去一个莎士比亚。
——丘吉尔《大国崛起大道思行》