我已经写好程式码了 但不知道如何利用 "constructor" 去改写程式码总共显示出1出生日期2.移动3.品种4.名子动物或狗在被創造出來時时,如何用建构式?求解改写程式码class Animal{ // public Animal(String birthday ) {// this.birthday = birthday ;// Move();// }// String birthday; void SetBirthday(String birthday){ this.birthday = birthday ; } void Show(){ System.out.println("birthday:" + birthday); } void Move(){ System.out.println("move "); }} class Dog extends Animal{ private String name; private String kind; void SetName(String name){ this.name = name; } void SetKind(String kind){ this.kind = kind ; } void Show(){ super.Show(); //birthday System.out.println("petname:"+name); System.out.println("petkind:"+kind); }} public class main{ public static void main(String[] args){ Dog dog = new Dog(); dog.SetBirthday("20060512"); dog.SetName("bread"); dog.SetKind("Golden Retriever"); dog.Show(); dog.Move(); Dog dog2 = new Dog(); dog2.SetBirthday("20070512"); dog2.SetName("Doge"); dog2.SetKind("Maltese"); dog2.Show(); dog2.Move(); Animal anAnimal = new Animal(); anAnimal.Move(); } }
添加回答
举报
0/150
提交
取消