super()用来创建父类对象
class People {
private int age;
private String name;
People() {
System.out.println(this.age);
System.out.println(this.name);
}
}
class Student extends People {
Student() {
super();
People p = new People();\\这里创建父类对象与super()功能相同。
}
}
public class Demo {
public static void main(String[] args) {
Student s = new Student();
}
}
this()用来调用同类的不同构造方法。
class People {
People() {
System.out.println("我是人");
}
}
class Student extends People {
Student(int a, String name) {
this(a);
System.out.println(a);
System.out.println(name);
}
Student(int b) {
super();
System.out.println(++b);
}
}
class Student2 extends People {
}
class Demo1 {
public static void main(String[] args) {
Student s = new Student(4, "李帅");
}
}
static,java专门为他提供一个空间静态方法区,创建的众多对象共享static修饰的数据
class People {
static String country;
int age;
}
public class Demo {
public static void main(String[] args) {
People p = new People();
People p1 = new People();
People p2 = new People();
p2.country = "中国";
p2.age=10;
System.out.println(p.country);
System.out.println(p1.country);
System.out.println(p2.country);
System.out.println(p.age);
System.out.println(p1.age);
System.out.println(p2.age);
}
}
static修饰的方法可以用类直接调用,不需要创建对象。
class People {
public static void play(){
System.out.println("打豆豆");
}
}
public class Demo {
public static void main(String[] args) {
People.play();
}
}