Java关键字(一)this与super关键字

Java关键字:

a、Java中一些赋以特定的含义、并用做专门用途的单词称为关键字,用来表示一种数据类型或数据结构;

b、Java中所有关键字都是小写的;

c、goto和const 虽然从未被使用,但也作为Java关键字保留;

d、关键字不能用作变量名、方法名、类名、包名和参数;

目前Java共有50个关键字:

final static this super public protected default privatevolatile synchronized abstract assert boolean break byte continue case catch char class const double do extends else float for goto long if implements import native new null instanceof int interface package return short strictfp  switch  while void throw throws transient try 

1、this关键字

表示类中的属性;可使用this调用本类的构造方法;this表示当前对象

(1)表示类中的属性

public class Person {
	private String name;
	private int age;
//	String name和name为实例化时传递过来的参数,this.name表示类的属性
	public Person(String name, int age){
		this.name = name;//为类的属性赋值
		this.age = age;
	}
	public String toString(){
		return "姓名:"+this.name+"年龄:"+this.age;
	}
}
public class ThisDemo01 {
	public static void main(String[] args) {
		Person per = new Person("张三", 20);
		System.out.println(per);
	}
}
输出结果:

姓名:张三年龄:20
(2)this调用本类的构造方法

注意:a、使用this调用构造方法只能放在构造方法的首行;b、程序中至少存在一个构造方法是不使用this调用其他构造方法的,一般都会将无参构造方法作为出口

public class Person {
	private String name;
	private int age;
//	定义无参构造方法
	public Person(){
		System.out.println("一个新的Person对象被实例化");
	}
	public Person(String name, int age){
		this();//此处调用Person的无参构造方法
		this.name = name;//为类的属性赋值
		this.age = age;
	}
	public String toString(){
		return "姓名:"+this.name+"年龄:"+this.age;
	}
}
public class ThisDemo01 {
	public static void main(String[] args) {
		Person per = new Person("张三", 20);
		System.out.println(per);
	}
}
输出结果:

一个新的Person对象被实例化
姓名:张三年龄:20
2、super关键字

super调用父类中的构造方法,放在子类构造方法的首行;访问父类的方法;访问父类的属性

实例

定义一个Person类

public class Person {
	private String name;
	private int age;
	public Person(){}
	public Person(String name, int age){
		this.name = name;
		this.age = age;		
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getInfo(){
		return "姓名:"+this.name+",年龄:"+this.age;
	}
}
子类Student继承父类,扩充:通过super()调用父类中的构造方法为属性赋值,子类新定义的属性在子类中定义赋值

public class Student extends Person{
	private String school;//子类新定义的属性
	public Student(String name, int age, String school){//子类及继承的弗雷德属性都传进来
		super(name,age);//调用父类的构造函数为name,age设置属性内容
		this.setSchool(school);
	}
	public String getSchool() {
		return school;
	}
	public void setSchool(String school) {
		this.school = school;
	}
//	重写父类中的方法
	public String getInfo(){
		return super.getInfo()+",学校:"+this.school;//扩充父类中的方法
	}
}
测试:

public class SuperDemo {
	public static void main(String[] args) {
		Student stu1 = new Student("张三",22,"清华大学");
		System.out.println(stu1.getInfo());
	}
}
输出结果:

姓名:张三,年龄:22,学校:清华大学

3、this与super关键字对比



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值