所以静态资源可以通过类名直接调用,即使没有创建对象也可以调用
package review.learn;
public class TestStatic2 {
public static void main(String[] args) {
System.out.println(Teacher.id);
Teacher.ready();
}
}
class Teacher{
String name;
static int id;
public void teach(){
System.out.println("teaching!");
}
public static void ready(){
System.out.println("preparing!");
}
}
上述name和teach函数没有用static修饰,所以要先定义对象才能调用
static修饰过的可以直接用类调用,不需要再定义对象
2.静态资源不能调用普通资源,但普通资源可以调用静态 3.多态- 多态的前提1:是继承
- 多态的前提2:要有方法的重写
- 父类引用指向子类对象,如:Animal a = new Cat();
- 多态中,编译看左边,运行看右边
package practise;
public class SelfTest {
public static void main(String[] args) {
Car c1=new Car();
System.out.println(c1.getBrand());
c1.stop();
c1.start();
BMW b=new BMW();
System.out.println(b.color+ b.color);
System.out.println(b.getName());//子类继承,可以查看父类成员变量
b.start();//子类里面有,所以执行子类的
b.stop();//子类没有,执行父类的
Car c2=new Tesla();//多态,左边父类,右边子类,
c2.start();//子类没有,执行父类的
c2.stop();//执行tesla
System.out.println(c2.getBrand());//子类没有 执行父类的
}
}
class Car{
private String brand;
private String name;
private double price;
private int id;
public void start(){
System.out.println("let's get started!");
}
public void stop(){
System.out.println("let's stop!");
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
class BMW extends Car{
String color="莫名其妙的黑";
@Override
public void start(){
System.out.println("get away i am bmw, i am gonna take off!");
}
// public void stop(){
// System.out.println("oh my god!i am too fast!i cannot stop!");
// }
}
class Tesla extends Car{
@Override
public void stop(){
System.out.println("oh my god!i am too fast!i cannot stop!");
}
public void swim(){
System.out.println("i can also swim how cool am i!");
}
}



