构造方法

package com.lv.test;

public class Cat {
    
    String name;
    int  age;
    
    public Cat(){
        
    }
    //带参数的构造器
    public Cat(String name){
        this.name =name;
    }
    
    public Cat(String name,int age){
        this(name);//不要加构造器cat名字
    }
    public void say(){
        //我的需求是  想要jack喵喵
        //想要使用当前对象的name 而不是这个局部变量的name
        
        String name="tom";
        //this 关键字代表当前对象
        //System.out.println(this.name+"喵喵喵");
        System.out.println(this.name+"喵喵喵");
    }
    
    public static void main(String[] args) {
        Cat cat1=new Cat();
        cat1.say();//为啥是null喵喵 因为String name;没有赋值
        
        Cat cat2=new Cat("jerry");
        cat2.say();
        
        Cat cat3=new Cat("tony");
        cat3.say();
    }
}

 

 

 

 

 

posted on 2020-02-17 20:33  代码吴彦祖  阅读(133)  评论(0)    收藏  举报