一、静态代码块
静态代码块不需要创建对象才执行,比对象数据优先存在于内存中
二、静态函数
三、单例设计模式
1.饿汉单例模式
缺点:一声明就创建一个对象,没有使用的话就浪费了。
2.懒汉单例模式
1.声明的时候没有赋值
2.创建对象的时候判断是否有值,无值创建一个新的对象,有值用之前的
3.推荐使用:饿汉单例设计模式,因为懒汉单例设计模式会存在线程安全问题,目前还不能保证一个类在内存中只有一个对象
四、继承
1.extends
class 类A Extends 类B
{
}
2.super 关键字代表父类空间的引用
可以通过super关键字调用继承父类的变量或方法
3.方法的重写和重载
class Student { String name; public Student(String name) { this.name=name; } public void study() { System.out.println(name+"学习马克思列宁主义"); } } class BaseStudent extends Student { public BaseStudent(String name) { super(name); } public void study() { System.out.println(name+"javase.."); } } class WorkStudent extends Student{ public WorkStudent(String name) { super(name); } public void study() { super.study(); System.out.println(name+"学习javase和android.."); } } class Demo1 { public static void main(String[] args) { // BaseStudent s=new BaseStudent("张三"); // s.study(); WorkStudent w=new WorkStudent("李四"); w.study(); } }
4.instanceof关键字
可以用来判断这个对象是否是某个类或继承某个类或有实现某个接口,是返回true,否返回false