static 关键字
static关键字是静态的意思,可以修饰成员方法,成员变量
static 修饰的特点:
- 被类的所有对象共享
- 可以通过类名调用,当然也可以通过对象名调用,推荐使用类名调用
static 访问特点:静态成员方法只能访问静态的成员变量和成员方法
非静态成员的访问方法
- 能访问静态的成员变量
- 能访问非静态的成员变量
- 能访问静态的成员方法
- 能访问非静态的成员方法
静态的成员方法 - 能访问静态的成员变量
- 能访问静态的成员方法
public class student {
public String name ;
public int age ;
public static String university;
public void show()
{
System.out.println(name + " " + age + " " + university);
}
}
public class studentDemo {
public static void main(String[] args) {
student.university = "henu" ;
student s1 = new student();
s1.name = "xlx";
s1.age = 20;
//s1.university = "henu";
s1.show();
student s2 = new student();
s2.name = "hyy";
s2.age = 20;
// s2.university = "henu";
s2.show();
}
}
多态
同一个对象,在不同时刻表现出来的不同形态
多态的前提和体现
- 有继承/实现关系
- 有方法重写
- 有父类引用指向子类对象
多肽中成员访问特点:
成员变量:编译看左边,执行看左边
成员方法:编译看左边,执行看右边
animal类
public class animal {
public int age = 40 ;
public void eat()
{
System.out.println("animal eat something");
}
}
cat类,父类是animal
public class cat extends animal{
public int age = 20 ;
public int weight = 10 ;
@Override
public void eat() {
System.out.println("cat eat fish");
}
public void playGame(){
System.out.println("cat is playing csgo");
}
}
测试类
public class animalDemo {
public static void main(String[] args) {
// 有父类引用指向子类对象
animal a = new cat();
System.out.println(a.age);
//System.out.println(a.weight); 报错
a.eat();
//a.playgame();
}
}
多态的转型
向上转型
- 从子到父
- 父类引用指向子类对象
向下转型
- 从父到子
- 父类引用转为子类对象
public class animalDemo {
public static void main(String[] args) {
// 有父类引用指向子类对象
animal a = new cat(); // 向上转型
System.out.println(a.age);
//System.out.println(a.weight); 报错
a.eat();
//a.playgame();
//创建cat类对象
/*cat c = new cat();
c.playGame();*/
//向下转型
cat c = (cat)a;
c.playGame();
}
}
抽象类
在java中,一个 没有方法体的方法应该被定义为抽象方法,而类中如果有抽象方法,该类必须定义为 抽象类。
抽象类特点:
抽象类和抽象方法必须使用abstrast关键字修饰
public abstract class 类名{}
public abstract void eat();
测试类
public class animalDemo {
public static void main(String[] args) {
// 有父类引用指向子类对象
animal a = new cat(); // 向上转型
//System.out.println(a.weight); 报错
a.eat();
a.sleep();
}
}
抽象类
abstract class animal {
public int age = 40 ;
public abstract void eat();
public void sleep()
{
System.out.println("sleep");
}
}
子类
public class cat extends animal{
public int age = 20 ;
public int weight = 10 ;
@Override
public void eat() {
System.out.println("cat eat fish");
}
public void playGame(){
System.out.println("cat is playing csgo");
}
}
抽象类的成员特点:
成员变量
- 可以是变量 也可以是常量
构造方法
- 有构造方法,但是不能实例化
- 构造方法用于子类访问父类数据的初始化
接口
接口就是一种 公共的标准规范标准,只要符合规范标准,大家都可以通用
java中的接口更多的体现在对行为的抽象
接口的特点:
- 接口用关键字interface修饰 public interfacce 接口名{}
- 类实现接口用implements表示 public class 类名 implements 接口名{}
- 多态的形式:具体类多态,抽象类多态,接口多态
测试类
public class jumppingDemo {
public static void main(String[] args) {
jumpping j = new cat();
j.jump();
}
}
接口
public interface jumpping {
public abstract void jump();
}
子类
public class cat implements jumpping{ //implement 实现
@Override
public void jump() {
System.out.println("cat can jump");
}
}
类和接口的关系
- 类和类的关系 继承关系,只能单继承,但是可以多层继承
- 类和接口的关系 实现关系,可以单实现,也可以多实现,还可以在继承一个类的同事实现多个接口
- 接口和接口的关系 继承关系,可以单继承,也可以多继承
测试类
public class interml extends Object implements inter1,inter2,inter3{ // 同时实现多个接口
}
接口
public interface inter3 extends inter1,inter2{
}
匿名内部类
格式:
new 类名或者接口名
{
重写方法;
}
本质:是一个继承了该类或者实现了该接口的子类匿名对象
测试类
public class outerDemo {
public static void main(String[] args) {
outer o = new outer();
o.method();
}
}
接口
public interface inter {
void show();
}
类
public class outer {
public void method(){
/*new inter() {
@Override
public void show() {
System.out.println(""); // 匿名内部类
}
};*/
/*new inter() {
@Override
public void show() {
System.out.println("niming"); // 匿名内部类
}
}.show();*/
inter i = new inter() {
@Override
public void show() {
System.out.println("niming"); // 匿名内部类
}
};
i.show();
i.show();
}
}