面向对象----构造方法(以手机为案例)

本文展示了一个简单的Java程序设计案例,定义了手机类及其属性(如品牌、颜色和号码),并提供了设置颜色和拨打电话的方法。通过实例化对象演示了如何使用该类。
package phone;

public class phone 
   {
  private String pinPai;//品牌
  public String getPinPai()
   {
     return pinPai;
    }
    private String haoma;//号码
    public String getHaoma() 
    {
        return haoma;
    }
    private String color;//颜色
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    //构造方法:构造对象的时候调用的方法
    public phone()
    {
        System.out.println("调用默认的构造方法");
    }
    //新的构造方法
    public phone(String pinPai,String haoMa){
        this.pinPai=pinPai;
        this.haoma=haoma;
    }
    
    
    
    
    
    
    
    public int call(String haom){
        System.out.println("拨打的电话号码为"+haom);
        
        int thsj =3;
        
        return thsj;
    }
}
package phone;

public class Test1 {

    public class phone {
        phone iphone6 = new phone();

        iphone6.setColor=("土豪金");

        iphone6.call("10086");
        // 新的构造方法

        phone huawei =new phone("华为""1234");
        
        System.out.println("品牌 ="+ huawei.getPinPai()+"号码="+hauwei.getHaoma());

    }

}

 

转载于:https://www.cnblogs.com/0927wyj/p/5034327.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值