第一个类是Dog类
第二个类是Puppy类
继承的作用就是可以直接调用父类的方法且不会对父类进行改变,如下例,可运行试试。
package NewGuy;
public class Puppy extends Dog{
Puppy(int a){
System.out.println("the name of puppy is "+ a);
}
public void bite(){
for(int i=1;i<10;i++){
System.out.println(i++);
}
}
public static void main(String[]args){
Puppy p=new Puppy(2+10);
p.age=2;
p.bite();
p.barking();
Dog dog=new Dog();
dog.age=3;
dog.barking();
}
}
package NewGuy;
public class Dog {
String bread;
int age;
String color;
public void barking(){
System.out.println("dog类被继承了"+age) ;
}
void hungry(){
}
void slepping(){
}
}