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声明,此变量会变为全局常量。
版权声明:本文为qq_41590178原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。