9.29-Static关键字与继承
Static关键字
特点
package day02;
public class StaticDemo {
public static void main(String[] args) {
Person.method();
Person p = new Person("张三",23);
p.show();
}
}
class Person{//描述中国人
private String name;
private int age;
static String country = "CN";//国籍 静态变量
public Person(String name,int age) {
this.name = name;
this.age = age;
}
/*
* ①静态方法中只能访问静态的成员
* ②静态方法中不能使用this和super关键字
*/
public /*static*/ void show() {
System.out.println
(Person.country+":"+this.name+":"+this.age);
}
public static void method() {
System.out.println(Person.country);
}
}成员变量与静态变量的区别
静态的使用场景
继承(extends)
例子
好处
Java中的单继承/多继承
在子父类中成员的特点体现
构造器
最后更新于
