首页 > 编程语言 > 详细

java-Java基础关键字this,supper,static的作用

时间:2021-06-10 11:47:57      阅读:14      评论:0      收藏:0      [点我收藏+]

java关键字:      this,supper

this: 当前/这个  

可修饰 属性/方法/构造器

修饰属性: 修饰成员变量(即 类变量)

代码:

public class User {

public int heda = 50;
public  void set(){
int heda = 10;
System.out.println("我是不加 [this] 输出结果: "+heda);
System.out.println("我是添加 [this.] 输出结果"+this.heda);
}}

 如果 当前类下的方法中  存在着与属性名相同的 (局部变量)  

不使用(this.)关键字修饰  那么调用只会是 此方法总存在的与属性名相同的变量;

技术分享图片

使用 [this.]  关键字

技术分享图片

如果 不存在与属性同名的变量   那么调用的是当前类的属性  

ps: 在调用属性时 是默认使用 [this.] 来调用的

 

 

修饰构造器:   格式为 this(参数名,......);     括号内的参数个数 根据自己需要调用的构造器参数个数 去判断     this调用构造器时   必修放在首行

代码:

public class User {
private String userName;
private Double height;
/*Integer 是 int 的包装类 等同于 int*/
private Integer age;

public User(){

}
User user = new User("person",19);
public User(String userName){
System.out.println("你好");
}

public User(String userName,Integer age){
this(userName);
this.age = age;
}}

 技术分享图片

 

 

 

 

修饰方法:

在当前类中调用本类方法时  默认使用[this.]修饰

 

 

 

supper 经常发生在父子类(即  继承中)

可修饰    属性 方法  构造器   

(作用基本于this相同)

修饰 属性

代表  父类中的属性  

修饰  方法  

代表   父类中的方法

修饰构造器

代表 父类构造器

 

static  静态的

static修饰的东西   会在类加载时  同时也会加载static修饰的 

可修饰   变量 方法  类  代码块  内部类

 

java-Java基础关键字this,supper,static的作用

原文:https://www.cnblogs.com/lxf-mw/p/14771493.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!