首页 > 其他分享 >this.关键字的小应用

this.关键字的小应用

时间:2022-10-25 10:56:52浏览次数:45  
标签:Customer shopping String System 关键字 应用 public name

public class test {
    public static void main(String[] args) {
        Customer n1 = new Customer("小明");
        n1.shopping();

        Customer n2 = new Customer("小红");
        n2.shopping();
    }
}

//定义一个顾客类
class Customer{
    //顾客的属性(实例变量)
    String name;

    //无参
    public Customer(){
    }
    //有参
    public Customer (String s){
        name = s;
    }

    //顾客的方法(实例方法)
    public void shopping(){
        //System.out.println(name+"正在购物中");
        //this.是可以省略的
        //this. 省略的话,还是默认访问“当前对象”的name。
        System.out.println(this.name+"正在购物中");
    }
}

 

标签:Customer,shopping,String,System,关键字,应用,public,name
From: https://www.cnblogs.com/Hangli123/p/16824124.html

相关文章