构造方法

package edu.xcdq;
public class Phone {
    public double width;
    public double high;
    public int weight;
    public String color;

    /*
    * 构造方法:没有返回值 方法的名字和类名需要完全一致
    * 通过构造方法,可以创建对象
    * */
    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,double j){

    }
    public void kaiji (double j , int i){

    }
    public void kaij (int i,int j,int k){

    }
    /*private int kaiji(){

    }*/
    public void guanji(){
        System.out.println("手机正在关机");
    }
    public String toString() {
        return "["+this.width + this.high +this.weight + this.color+ "]";

    }
}


©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容