super与final关键字

super关键字可以从子类中调用父类的构造方法,普通方法和属性。
调用父类构造器时必须放在子类构造器首行。

public class SuperDemo01 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Student s=new Student("张三",20);
		System.out.println(s.print());
	}

}
class Person{
	
	private String name;
	
	public Person(String name) {
		this.setName(name);
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public String superPrint() {
		return "姓名是:"+this.getName();
	}
	
}
class Student extends Person{
	
	private int age;
	
	public Student(String name,int age) {
		super(name);
		this.setAge(age);
	}
	
	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
	
	public String print() {
		return super.superPrint()+",年龄为:"+this.getAge();
	}
}
//结果:
//姓名是:张三,年龄为:20

final关键字
在Java中final关键字代表最终的意思。
1、使用final关键字声明的类不能有子类
2、使用final关键字声明的方法不能被覆写。
3、使用final关键字声明的变量为常量,常量不能修改。要求此常量字母全为大写。

如果程序中的变量使用public static final声明,此变量会变为全局常量。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值