【Java笔记】面向对象编程-类与对象(一)-面向对象编程、类与对象的定义与使用、封装、构造方法、this关键字、static关键字 - 总结二

面向对象编程-类与对象(上)

备注:

定义类  范例1:

class Person{
    //属性
    public String name;
    public int age;
    //构造方法
    public Person(String name,int age){
        this.name = name ;
        this.age = age ;
    }
    //行为
    public String getPersonInfo(){
        return "姓名:"+this.name+",年龄:"+this.age;
    }
}

定义对象   范例2:

Person p1 = new Person();
Person p2 = new Person("Steven",25);

通过对象调用实例变量与实例方法    范例3:

//调用类的属性
System.out.println(p.name);
//调用类的方法
System.out.println(p.getPersonInfo());

构造方法定义原则  范例4:

//无参的构造方法
public Person(){}

//带参的构造方法
public Person(String n,int i){
      name = n ;
      setAge(i);
}

构造方法重载   范例5:

public Person(){
System.out.println("===无参构造===");
}
public Person(String n){
name = n ;
System.out.println("===有参构造===");
}

匿名对象  范例6:

new Person("张三",20).getPersonInfo();

this调用本类属性  范例7:

this.name = name ;
this.age = age ;

调用普通方法   范例8:

this.print();//print()在本类中

调用构造方法   范例9:

this();//调用本类无参构造
this(name);//调用本类有参构造

类与对象的定义与使用:

public class Car{

     //属性
     //发动机编号
     private String engineNumber;

     //品牌
     private String brand;

     //颜色
     private String color;
     
     //true 自动挡  false 手动挡
     private boolean autoType;
     
     //构造方法 
     //方法的参数名:符合标识符命名规范  简明思议
     public Car(String engineNumber){
         this.engineNumber = engineNumber;
     }
     
     //getter setter
     //现阶段就这么写,自动生成
     //this 该类实例化的对象,即当前操作的对象
     public String getEngineNumber(){
         return this.engineNumber;
     }
     
     public String getBrand(){
         return this.brand;
     }
     
     public void setBrand(String brand){
         this.brand = brand;
     }
     
     public String getColor(){
         return this.color;
     }
     
     public void setColor(String color){
         this.color = color;
     }

     //属性是boolean类型的getter方法 => isXxx
     public boolean isAutoType(){
         return this.autoType;
     }
     
     public void setAutoType(boolean autoType){
         this.autoType = autoType;
     }
     
     public String getCarDescription(){
         return "发动机编号:"+this.getEngineNumber() +"\n"
         + "品牌:"+this.brand+"\n"
         + "颜色:"+this.color+"\n"
         + "是否自动挡:"+this.isAutoType();
     }
     
     //行为(功能)
     public void drive(){
         System.out.println("向前开 喝酒不开车,开车不喝酒");
         //结合方法使用
         if(this.isAutoType()){
             System.out.println("自动挡,好开");
         }else{
             System.out.println("手动挡,手疼");
         }
     }
     
     public void back(){
         System.out.println("倒车 请注意");
     }
     
     
     
     public static void main(String[] args){
         /*
         Car  wlhg = new Car("WL0010");
         wlhg.drive();
         wlhg.setAutoType(true);
         wlhg.drive();
         wlhg.setColor("白色");
         wlhg.setBrand("五菱宏光");
         
         String desc = wlhg.getCarDescription();
         System.out.println(desc);
         */
         
         //数组
         //1.静态初始化
         //2.动态初始化
         Car[] cars = new Car[]{
                new Car("WL00110"),
                new Car("HX00111"),
         };
         //赋值操作
         cars[0].setBrand("");         
         
     }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值