JAVA改写成用constructor方法

 乌海阿斯顿 发布于 2022-10-29 05:45

我已经写好程式码了 但不知道如何利用 "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();
    
        }
    }
1 个回答
  • class Animal {
        String birthday;
        Animal(String birthday) {
            this.birthday = birthday;
        }
        //...
    }
    class Dog extends Animal {
        String name;
        String kind;
        Dog(String birthday, String name, String kind) {
            super(birthday);
            this.name = name;
            this.kind = kind;
        }
        //...
    }
    public class Main() {
        public static void main(String[] args) {
            Dog dog = new Dog("20060512", "bread", "Golden Retriever");
            dog.show();
            dog.move();
            //...
        }
    }
    2022-10-30 13:50 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有