问答详情
源自:9-7 Java 中的 super 的使用

是不是做错了 感觉不对呢 大佬指点一下


是不是做错了 感觉不对呢 大佬指点一下https://img2.mukewang.com/5d240bd20001dc5406160212.jpg

https://img2.mukewang.com/5d240bd80001effb06390229.jpg

https://img2.mukewang.com/5d240bd80001e9c606210179.jpg

https://img1.mukewang.com/5d240bd800010d2906260194.jpg

https://img2.mukewang.com/5d240bdb0001660505620309.jpg



提问者:王大哈哈i 2019-07-09 11:37

个回答

  • 唯进
    2019-07-09 14:55:01
    已采纳

    可以用构造方法创建对象的时候往里面写数据,另外,父类的属性子类可以直接用 this. 就行了

    public Bus(Int people,String way){

    this.people = people;

    this.way = way;

    }

    //创建对象,和构造方法对应

    Vehicle obj1 = new Bus(40,"公交");

    obj1.Vway();

  • 慕粉2235138145
    2020-04-29 10:06:13

    public class HelloWorld {    public int people;    public String way;    public void Abc(){            System.out.println("交通工具为:"+way+"\n乘车人数:"+people             );            }   public class bus extends HelloWorld{    public   bus(int people,String way){	   this.people=people;       this.way=way;      this.Abc();    }   }public class run extends HelloWorld{    public   run(int people,String way){	   this.people=people;       this.way=way;       this.Abc();    }   }	public static void main(String[] args) {        		 		HelloWorld num=new HelloWorld();		bus nums=num.new bus(40,"公交");        run numss=nums.new run(4,"跑步");	    	}}


  • 慕粉2235138145
    2020-04-29 10:05:48

    public class HelloWorld {

        public int people;

        public String way;

        public void Abc(){

           

         System.out.println("交通工具为:"+way+"\n乘车人数:"+people             );

            

        }

       public class bus extends HelloWorld{

        public   bus(int people,String way){

       this.people=people;

           this.way=way;

          this.Abc();

        }


       }

    public class run extends HelloWorld{

        public   run(int people,String way){

       this.people=people;

           this.way=way;

           this.Abc();

        }


       }





    public static void main(String[] args) {

            

     

    HelloWorld num=new HelloWorld();

    bus nums=num.new bus(40,"公交");

            run numss=nums.new run(4,"跑步");

        

    }

    }