java中this关键变量

java中this关键变量,第1张

java中this关键变量 案例代码
public class Car {
    String color;
    int speed;
    int seat = 5;
    public void run(){
        System.out.println("车能跑");
        System.out.println(this.color);
        System.out.println(this.seat);

    }

    public static void main(String[] args) {
        Car c = new Car();
        c.color = "红色";
        c.speed = 130;

        c.run();//在调用方法的时候,java会自动把对象传递给方法,在方法中
        //有this来接受对象

    }

}

小结

this 可以在方法体内部对参数进行调用,与Python里的self.变量名,的用法类似。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5521918.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-13
下一篇2022-12-13

发表评论

登录后才能评论

评论列表(0条)

    保存