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

来源:9-7 Java 中的 super 的使用

王大哈哈i

2019-07-09 11:37


是不是做错了 感觉不对呢 大佬指点一下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



写回答 关注

3回答

  • 唯进
    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();

    慕容0262...

    Vehicle obj1 = new Bus(40,"公交");和Vehicle obj1 = new Bus("公交",40);输出是一样的吗?

    2019-08-28 21:47:53

    共 5 条回复 >

  • 慕粉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,"跑步");

        

    }

    }


Java入门第二季 升级版

课程升级!以终为始告别枯燥,在开发和重构中体会Java面向对象编程的奥妙

530105 学习 · 6086 问题

查看课程

相似问题