2021-04-20

public class Phone {

public double width;

    public double high;

    public double weight;

    public Stringcolor;

    /**

    * 构造方法:没有返回值 方法的名字和类名需要完全一致

    * 通过构造方法,可以创建对象

    */

    public  Phone(){

System.out.println("我被创建了");

    }

/*

    构造方法

    含有全部的参数*/

/* public Phone(double kuan ,double gao, int zhongliang,String yanse){

width=kuan;

high=gao;

weight=zhongliang;

color=yanse;

}*/

    public Phone(double width,double high,int weight,String color){

this.width=width;

        this.high=high;

        this.weight=weight;

        this.color=color;

}

/*

  构造函数

  含有全部的参数*/

public Phone(double kuan,double gao, int zhongliang) {

width = kuan;

    high = gao;

    weight = zhongliang;

}

/**

* 方法的重载

* 要求:1 方法的名字相同 2 参数不同

*                    2.1参数的类型不同

*                    2.2参数的个数不同

*                    2.3参数类型的调用顺序不同

*/

public void kaiji(){

System.out.println("手机正在开机");

}

public  void kaiji(int i,int j  ){

}

public  void kaiji(double j,int i){

}

public  void kaiji(int i,int j,int k){

}

/*private void kaiji(){

}*/

    public void guanij(){

System.out.println("手机正在关机");

    }

public StringtoSting(){

System.out.println();

        return "[" +this.width+this.high+this.weight+this.color +"]";

    }

}

测试类

public class Demo01 {

public static void main(String[] args) {

//new申请内存

        Phone xiaomi11=new Phone();

        xiaomi11.width=15;

        xiaomi11.high=155;

        xiaomi11.weight=366;

        xiaomi11.color="香槟银";

        Phone xiaomi11tra=new Phone(16,156,366);

        Phone iphone=new Phone(17,157,357);

        System.out.println(xiaomi11);

        System.out.println(xiaomi11tra);

        System.out.println(iphone);

    }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容