Dog.java
- 提问者网友:謫仙
- 2021-07-17 23:48
说明一:关于Dog
两个属性,狗的名字(dogName,String),狗的年龄(dogAge,int)
一个方法,dogWalk(),打印一句话简单描述狗走路的样子
一个构造方法,无参数,在此构造方法中初始化dogName,dogAge
说明二:关于SmallDog
继承Dog,不添加任何属性和方法
最后,在测试类中的main方法中生成SmallDog类的对象,访问属性dogName和dogAge,并打印出他们的值;调用dogWalk(),看看效果
- 五星知识达人网友:山河有幸埋战骨
- 2021-07-18 00:22
//DogDemoMain.java
public class DogDemoMain {
public static void main(String[] args) {
// TODO Auto-generated method stub
SmallDog sdog = new SmallDog();
System.out.println("The name :" + sdog.getDogName() + " The age :" +
sdog.getDogAge());
sdog.dogWalk();
}
}
//Dog.java
public class Dog {
private String dogName;
private int dogAge;
public Dog(){
dogName="dog";
dogAge=4;
}
public void dogWalk(){
System.out.println("狗走路的样子");
}
public String getDogName() {
return dogName;
}
public void setDogName(String dogName) {
this.dogName = dogName;
}
public int getDogAge() {
return dogAge;
}
public void setDogAge(int dogAge) {
this.dogAge = dogAge;
}
}
//SmallDog.java
public class SmallDog extends Dog {
public SmallDog(){
super();
}
}
- 1楼网友:话散在刀尖上
- 2021-07-18 01:44
package wenwen;
class Dog { String dogName; int dogAge; Dog() { dogName = "Pheebe"; dogAge = 2; } public void dogWalk() { System.out.println("Dog walks by its four legs"); } }
class SmallDog extends Dog { }
public class DogTest { public static void main(String[] args) { SmallDog sd = new SmallDog(); System.out.println("The dog's name is: " + sd.dogName); System.out.println("The dog's age is: " + sd.dogAge); sd.dogWalk(); } }