要点回顾:
1、final:
a.最终类:不能被继承,没有子类。
b.最终方法:不允许再以任何形式进行修改,在继承中子类不能覆盖父类的最终方法。
c.final修饰的变量相当于常量,且必须在定义时进行赋值。
2、抽象类:不能用来创建对象
注意:
a.抽象类可以没有抽象方法,通常至少有一个。
b.一个类中含有抽象方法,该类必定是抽象类。
c.private和static修饰的方法不能声明为抽象方法。
d.继承抽象类之后,只有实现了抽象类中所有抽象方法的子类才能是一个可以创建对象的类,否则还是抽象类。
3、接口 :里面的方法都是抽象方法
注意:
a.接口中的成员变量是public、static、final的;接口中的成员方法都是public、abstract的。
b.实现方法时必须加上public修饰。
实验与训练:
1、去掉final就行了哈~
package train8_1;//去掉两个final
class Person{
String name;
char sex;
Person(){}
Person(String n,char s){
name=n;
sex=s;
}
void show() {
System.out.println("name is "+name+",sex is "+sex);
}
}
class Student extends Person{
int number;
Student(){}
Student(String n,char s,int num){
name=n;
sex=s;
number=num;
}
void show() {
System.out.println("name is "+name+",sex is "+sex+",number is "+number);
}
}
class Pupil extends Student{
double hcScore;
Pupil(){}
Pupil(String n,char s,int num,double hcs){
name=n;
sex=s;
number=num;
hcScore=hcs;
}
void show() {
System.out.println("name is "+name+",sex is "+sex+",number is "+number+",Score is "+hcScore);
}
}
public class Train8_1 {
public static void main(String[] args) {
Person p=new Person("小明",'男');
p.show();
Student stu=new Student("小明",'男',101);
stu.show();
Pupil pu=new Pupil("小明",'男',101,95);
pu.show();
}
}
2、
package train8_2;
import java.util.*;
interface IShape{
public double area();
}
class Square implements IShape{
double length;
Square(double l){
length=l;
}
public double area() {
return length*length;
}
}
public class Train8_2 {
public static void main(String[] args) {
Scanner reader=new Scanner(System.in);
System.out.println("请输入正方形的边长:");
Square test=new Square(reader.nextDouble());
System.out.println("该正方形的面积是:"+test.area());
}
}