package com.qfedu.b_abstract;/*** LOL英雄类, 每一个英雄都有QWER技能* @author Dream**//** 第一步:* 使用abstract关键字修饰要求子类重写的方法* 【方法报错】* Abstract methods do not specify a body* abstract修饰的方法没有方法体* 【Eclipse快速修复】* Ctrl + 1* 选择Remove Method Body 删除方法体* 第二步:* 【方法报错】* The abstract method q in type LOLHero can only be defined by an abstract class* LOLHero类内的abstract修饰方法q(), 有且只能定义在一个abstract修饰的类内* 【类名报错】* The type LOLHero must be an abstract class to define abstract methods* abstract修饰的方法, 必须在定义再abstract修饰的类内或者.........* 【Eclipse快速修复】* Ctrl + 1* 选择Make Type 'LOLHero' abstract 使用abstract修饰LOLHero类* 第三步:* 【子类报错】* The type Thresh must implement the inherited abstract method LOLHero.q()* Thresh类必须实现继承LOLHero类内的abstract方法q* 因为abstract修饰的方法, 在父类abstract类内没有方法体, 子类如果想要使用abstract修饰的方法* 必须完成方法体的实现* 【Eclipse快速修复】* Ctrl + 1* 选择Add unimplemented methods 添加未实现方法*/abstractclassLOLHero{abstractpublicvoidq();abstractpublicvoidw();abstractpublicvoide();abstractpublicvoidr();}/*** 锤石类继承LOLHero类* @author Dream**/classThreshextendsLOLHero{@Overridepublicvoidq(){System.out.println("死亡判决");}@Overridepublicvoidw(){System.out.println("魂引之灯");}@Overridepublicvoide(){System.out.println("厄运钟摆");}@Overridepublicvoidr(){System.out.println("幽冥监牢");}}/*** 维鲁斯类继承LOLHero类* @author Dream**/classVarusextendsLOLHero{@Overridepublicvoidq(){System.out.println("穿刺之箭");}@Overridepublicvoidw(){System.out.println("枯萎箭袋");}@Overridepublicvoide(){System.out.println("恶灵箭雨");}@Overridepublicvoidr(){System.out.println("腐败链锁");}}publicclassDemo1{publicstaticvoidmain(String[] args){Thresh saolei =newThresh();saolei.q();saolei.e();saolei.w();saolei.r();System.out.println("-----------------");Varus varus =newVarus();varus.q();varus.w();varus.e();varus.r();}}
package com.qfedu.e_final;/*
final关键字可以修饰局部变量 有且只能被赋值一次, 赋值之后不能修改成员变量 定义时必须初始化, 未初始化报错成员方法 使用final修饰的方法为最终方法, 不能被重写类 没有子类 不能被继承*/finalclassFather{finalpublicvoidgame(){System.out.println("黄金矿工");}}// The type Son cannot subclass the final class Father// Son类不能是final修饰的类Father类的子类, 不能继承Fahter// class Son extends Father {// The blank final field age may not have been initialized// 使用final修饰的成员变量还没有被初始化// final int age = 10;// Cannot override the final method from Father// 使用final修饰的方法为最终方法, 不能被重写// public void game() {// System.out.println("Pubg");// }// }publicclassDemo1{publicstaticvoidmain(String[] args){finalint num;num =10;// The final local variable num may already have been assigned// 使用final修饰的局部变量num已经被赋值// num = 20;}}
7.2 思考题
package com.qfedu.e_final;classDog{String name;int age;}publicclassDemo2{publicstaticvoidmain(String[] args){Dog dog =newDog();dog.name ="八公";dog.age =15;/** final 修饰的是dog1, dog1是一个类对象, 同时是一个引用数据类型的变量* dog1存储数据不可以改变!! dog1只想不可以改变, 但是dog1指向空间中的内容可以改变*/final Dog dog1 =newDog();// dog1 能不能操作成员变量?dog1.name ="骚杰";dog1.age =16;// 能不能修改??dog1.name ="123";dog1.age =20;// The final local variable dog1 cannot be assigned. // It must be blank and not using a compound assignment// dog1 = new Dog();}}