JAVA改写成用constructor方法
PHP中文网
PHP中文网 2017-04-17 17:54:17
0
1
412

我已经写好程式码了 但不知道如何利用 "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();
    
        }
    }
PHP中文网
PHP中文网

认证高级PHP讲师

全部回复(1)
伊谢尔伦

雷雷

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!