什么是抽象:
像某件东西,却又不是那个东西,具有某种对象的特征,但又不完整.
在程序中,有些方法父类没有实现,交给子类实现
基本使用:
创建一个父类,带抽象方法
//类必须要使用abstract关键字进行修饰
public abstract class Father {
int money;
public Father(){
money =10000;
}
//抽象方法,没有方法实现体
public abstract void costMoney();
}
创建一个子类,继承父类,必须实现抽象方法
public class Son extends Father{
@Override
public void costMoney() {
System.out.println("子类买了一辆宝马");
}
}
在使用的时候,可以使用父类引用也可以用子类引用,但是只能new出来子类对象.
public static void main(String[] args) {
// Father son = new Son();
Son son = new Son();
son.costMoney();
// Father father = new Father();
}
抽象类的本质:
就是强制要求子类重写方法
使用技巧,作为父类,可以定义一个抽象的方法,提前调用他让子类实现
//掉落的抽象方法
public abstract void lost();
public void kill(Father target){
//击败了某个对手
System.out.println("击败了"+target.name);
//对手死亡之后会又不同奖励
target.lost();
}
抽象方法的特别用法(不创建子类);
Father father = new Father("小怪") {
@Override
public void costMoney() {
}
@Override
public void lost() {
System.out.println(this.getName()+"掉了"+this.getMoney()+"金币");
}
};
father.kill(father);
static:
概念:
static是java中特殊的关键字,主要是用来修饰属性,方法和类.
作用可以是java不用new出对象换也可以执行
public class MyObject {
//没有用static修饰
public void method1(){
System.out.println("我是方法1");
}
//用了static修饰
public static void method2(){
System.out.println("我是方法2");
}
}
static修饰的方法不需要new也能调用
public static void main(String[] args) {
//method2可以直接使用,不需要new
MyObject.method2();
//method1必须new对象才可以使用
MyObject obj = new MyObject();
obj.method1();
obj.method2();
}
static的基本原理
java程序一开始运行就会加载静态代码区的内容
只需要静态代码区的方法和属性的地址就能直接调用
静态区的代码会永远存在,不能被删除,这样子会导致很多内存被浪费掉
为了跟简单,可以直接在静态代码中编写
static{
System.out.println("我是静态代码块");
}
由于static在静态代码区中
所以静态代码中不能通过this调用其他方法和变量
可以使用局部变量
可以使用静态修饰的变量
static修饰变量
static可以修饰成员变量(不能修饰局部变量)
被修饰的成员变量是在静态区的内存中
程序被加载是就会存在,而且不能被消除
当前类创建其他的所有对象,都会共享这一个static变量
被static修饰的变量叫做全局变量,可以通过类名,变量名,在任意类中直接使用
static修饰内部类
在类中定义的类,叫做内部类
在默认情况下,内部类可以直接调用外部类的成员变量
public class Student {
String name;
int money;
Computer computer;//电脑属性
public Student(){
money = 10000;
computer = new Computer();
}
public void buyComputer(int money){
computer.cost(money);
System.out.println("还剩下:"+this.money);
}
public class Computer{
//电脑的开销
public void cost(int money){
Student.this.money = Student.this.money - money;
System.out.println("购买电脑花费了:"+money);
}
}
}
内部类默认情况下会保留一个外部类的this引用,使得内部类可以不用传引用就可以调用外部类的成员变量
垃圾回收期,回收内存的前提是没有其他的引用指向它
内部类的默认引用永远指向外部类,从而导致内存泄漏
可以使用static防止内存泄漏
public class Student {
String name;
int money;
Computer computer;//电脑属性
public Student(){
money = 10000;
computer = new Computer(this);
}
public void buyComputer(int money){
computer.cost(money);
System.out.println("还剩下:"+this.money);
}
public static class Computer{
SoftReference<Student> student;
public Computer(Student student){
this.student = new SoftReference<>(student);
}
//电脑的开销
public void cost(int money){
//name = "电脑达人";
this.student.get().money = this.student.get().money - money;
System.out.println("购买电脑花费了:"+money);
}
}
}
在使用static之后,就不会自动传递外部类的引用,但是如果一定使用外部类成员变量,可以手动传递外部类的引用,通过软引用进行保存
软引用保存的内存地址,会在这个内存长期不被使用的时候,由JVM自动释放
final
概念:
final修饰的类不能被继承
final修饰的方法不能被重写
final修饰的变量不能被修改
final修饰变量
final修饰的变量只能被赋值一次
作用:
防止变量被修改
在内部类中防止外部变量被修改,可以是用final修饰变量
public static void main(String[] args) {
final int money = 10;
Father father = new Father("测试") {
@Override
public void costMoney() {
System.out.println(money);
}
@Override
public void lost() {
}
};
}
在定义常量的时候使用final,防止代码中出现幻数
public static final String FEMALE = "女";
public static final int YOUNG
被final修饰的类不能被继承