? ?//演示final修饰变量
? ? ?class Aoo{
? ? ? ? ?final int num = 5;
? ? ? ? ?void show(){
? ? ? ? ? ? ?//num = 55; //编译错误,final的变量不能被改变
? ? ? ? ?}
? ? ?}
? ? ?//演示final修饰方法
? ? ?class Boo{
? ? ? ? ?final void show(){}
? ? ?}
? ? ?class Coo extends Boo{
? ? ? ? ?//void show(){} //编译错误,final修饰的方法不能被重写
? ? ?}
? ? ?//演示final修饰类
? ? ?final class Doo{}
? ? ?//class Eoo extends Doo{} //编译错误,final的类不能被继承
? ? ?class Foo{}
? ? ?final class Goo extends Foo{} //不能当老爸,但能当儿子
? ?- 必须声明同时初始化
? ?- 通过类名点来访问,不能被改变
? ?- 建议:常量名所有字母都大写,多个单词用_分隔
? ?- 编译器在编译时会将常量直接替换为具体的值,效率高
? ?- 何时用:数据永远不变,并且经常使用
? ?public class StaticFinalDemo {
? ? ? ?public static void main(String[] args) {
? ? ? ? ? ?System.out.println(Hoo.PI); //通过类名点来访问
? ? ? ? ? ?//Hoo.PI = 3.1415926; //编译错误,常量不能被改变
? ?
? ? ? ? ? ?//1)加载Ioo.class到方法区中
? ? ? ? ? ?//2)将静态变量num一并存储到方法区中
? ? ? ? ? ?//3)到方法区中获取num的值并输出
? ? ? ? ? ?System.out.println(Ioo.num);
? ?
? ? ? ? ? ?//编译器在编译时将常量直接替换为具体的值,效率高
? ? ? ? ? ?//相当于System.out.println(5);
? ? ? ? ? ?System.out.println(Ioo.COUNT);
? ? ? ?}
? ?}
? ?
? ?class Ioo{
? ? ? ?public static int num = 5; //静态变量
? ? ? ?public static final int COUNT = 5; //常量
? ?}
? ?
? ?class Hoo{
? ? ? ?public static final double PI = 3.14159;
? ? ? ?//public static final int NUM; //编译错误,常量必须声明同时初始化
? ?}
? ?- 由abstract修饰
? ?- 只有方法的定义,没有具体的实现(连{}都没有)
? ? ?抽象类和抽象方法的用法:
abstract class Shape {
protected String color;
public Shape(String color) {
this.color = color;
}
public abstract double getArea();
public void display() {
System.out.println("This shape has color: " + color);
}
}
class Circle extends Shape {
protected double radius;
public Circle(String color, double radius) {
super(color);
this.radius = radius;
}
@Override
public double getArea() {
return Math.PI * radius * radius;
}
}
class Rectangle extends Shape {
protected double length;
protected double width;
public Rectangle(String color, double length, double width) {
super(color);
this.length = length;
this.width = width;
}
@Override
public double getArea() {
return length * width;
}
}
public class Main {
public static void main(String[] args) {
Circle circle = new Circle("Red", 5.0);
circle.display();
System.out.println("Area: " + circle.getArea());
Rectangle rectangle = new Rectangle("Blue", 4.0, 6.0);
rectangle.display();
System.out.println("Area: " + rectangle.getArea());
}
}
? ? ? ? 在以上代码中,我们定义了一个抽象类Shape
,它包含一个抽象方法getArea()
和一个普通方法display()
。抽象方法没有具体的实现,而是由具体的子类来实现。我们还定义了两个具体的子类Circle
和Rectangle
,它们继承自Shape
类并实现了getArea()
方法。在Main
类中,我们创建了一个圆和一个矩形对象,并调用它们的display()
和getArea()
方法来展示多态的效果。
? ?- 由abstract修饰
? ?- 包含抽象方法的类必须是抽象类
? ?- 抽象类不能被实例化(new对象)
? ?- 抽象类是需要被继承的,派生类:
? ? ?- 重写所有抽象方法--------------变不完整为完整
? ? ?- 也声明为抽象类------------------一般不这么做
? ?- 抽象类的意义:
? ? ?- 封装共有的属性和行为--------------------代码复用
? ? ?- 为所有派生类提供统一的类型-----------向上造型---代码复用
? ? ?- 可以包含抽象方法,为所有派生类提供统一的入口(能点出来)
? ? ? ?派生类的行为不同,但入口是一致的,同时相当于定义了一个标准
? ? ? ?静态变量(Static Variables)是属于类的变量,它们在类被加载时初始化,并且在整个类的实例之间共享。静态变量使用static关键字声明。其他类的实例可以通过类名直接访问静态变量,不需要创建对象的实例。
? ? ? ?静态变量的特点:
? ? ? 抽象方法(Abstract Methods)是没有具体实现的方法,只有方法的声明,没有方法体。抽象方法使用abstract关键字声明。抽象方法必须在抽象类中声明,抽象类一般用于定义一组相关的类的共同行为和属性。
? ? ?抽象方法的特点: