final 关键字
可以修饰类,属性,方法
1、修饰变量(成员变量或局部变量),则称为常量.只能赋值一次
2、修饰方法,该方法不能被重写
3、修饰类,则该类不能被继承
--------------------------------------------------------------
final 修饰的类表示,这个类是最终类,是无法被继承的,也就是说它没有子类
运用场景:不想让其有子类的场景下使用(处于安全的考虑)
final 修饰方法 表示该方法是最终方法,无法被子类进行重写,但是可以继承
运用场景:不想让其有子类的修改父类核心功能
public /*final */class Father {
//String
public final void info(){
System.out.println("Father.info()");
}
}
-------------------------------------------------------------------------
/*
* final 修饰类:
* 1、表示类是最终类,不能被继承
* 2、运用场景:不想让其有子类的场景下使用(处于安全的考虑)
*
*
* final 修饰属性
* 1、声明的时候必须指定具体的
* 2、基本类型,在指定具体的值后,无法修改
* 3、引用类型,在指定具体的值后,无法修改,但是,可以修改引用对象的具体属性(非final)
* 4、运用场景:在内部类使用
*
* final 修饰方法
* 1、表示最终方法,不能被子类重写,可以继承
* 2、运用场景:不想让其有子类的修改父类核心功能
*
* final 跟 static 配合使用
* 1、表示常量
* 2、运用场景: 系统参数的标识 1 / 0
*
*/
public class FinalDemo {
public final static String TAG = "FinalDemo" ;
/**
* Multiple markers at this line
- The value of the field FinalDemo.intField is not
used
- The blank final field intField may not have been
initialized
*/
//final修饰的 基本数据类型的变量(成员或局部)时,必须指定具体的值
private final int intField = 1;
//The blank final field son may not have been initialized
//final修饰的 引用类型的变量时,变量指定内存地址不能改变,但是,对象中的属性
//只要不是final修饰的属性,都可以改变
public final Son son = new Son();
//shift + ctrl + x
public final static int SYSTEM_PARAM = 1; //常量
public final static int LOGIN_STATUS_SUCCESS = 1; //常量
public static void main(String[] args) {
final int intField2 = 2;
FinalDemo finalDemo = new FinalDemo();
//The final field FinalDemo.intField cannot be assigned
//final 修饰的基本类型的变量(成员或局部)时,一旦赋值了,那么就无法重新赋值了
//finalDemo.intField = 2;
//intField2 = 3;
finalDemo.son.name = "这是被改后的son 的name值";
//The final field FinalDemo.son cannot be assigned
//finalDemo.son = new Son();
}
}
-----------------------------------------------------------------------
java.lang.Object 类是所有类的父类
组合 跟 继承
class Father{
public void info(){
}
}
class Son extends Father{ //继承
}
class NoSon{ //组合/聚合
Father father = new Father();
public void info(){
father.info();
}
}
class Test{
main(){
Son son = new Son();
son.info();
NoSon noson = new NoSon();
noson.info();
}
}
运用场景:如果2个类有父子关系的时候,用继承,其他情况下使用组合
---------------------------------------------------------------------------
向上转型 和 向下转型
public class Pet {
}
public class Cat extends Pet {
}
public class Dog extends Pet {
}
public class Test {
public static void main(String[] args) {
Pet pet = new Pet();
//向上转型
Pet pet2 = new Dog();
//向下转型 需要强转,需要自己承担风险
Dog dog2 = (Dog) pet;
Cat cat = (Cat) pet;
}
}
/**
* 向上转型:eg:Pet pet = new Dog("旺财", "狗");
* 1、不需要强制转换
* 2、可以访问父类的方法,属性
* 3、无法访问子类特有的方法及属性(对子类方法或属性进行遮蔽)
*
* 向下转型:
* 1、需要强制转换
* 2、如果把一个类型转换成其他类型,编译没有报错,执行报错:
* java.lang.ClassCastException --类转换异常
* 3、如果把一个类型转换成其他类型,然后再由其他类型转换会本类型,编译执行都通过
*/
-------------------------------------------------------------------
instaceof 和向上转型 和 向下转型 配合使用
instaceof 关键字 --> 用来判断该引用类型变量所"指向"的对象是否赋予该类
-----------------------------------------------------------------------------------------
1、父类
public class Pet {
public String name;
public String type;
public Pet(){}
public Pet(String name, String type) {
super();
this.name = name;
this.type = type;
}
public void eat(){
System.out.println("Pet 吃饭了。。。");
}
}
2、子类
public class Bird extends Pet{
public String color = "red";
public Bird(String color) {
this.color = color;
}
public Bird(String name, String type) {
super(name, type);
}
public void eat(){
System.out.println("鸟 吃虫。。。");
}
public void fly(){
System.out.println("鸟 飞得更高。。。");
}
3、子类
public class Dog extends Pet {
public String voice = "汪汪";
public Dog(String voice) {
this.voice = voice;
}
public Dog(String name, String type) {
super(name, type);
}
public void look(){
System.out.println("Dog 看门。。。");
}
//犬吠
public void bark(){
System.out.println(super.name + "汪汪地叫了....");
}
public void eat(){
System.out.println("Dog 吃G(c)T(s)。。。");
}
}
4、子类
public class Cat extends Pet{
public String life = "9条命...";
public Cat(){}
public Cat(String life) {
this.life = life;
}
public Cat(String name, String type) {
super(name, type);
}
public void eat(){
System.out.println("Cat 吃鱼。。。");
}
public void catchL(){
System.out.println("Cat 抓老鼠....");
}
@Override
public String toString() {
return "Cat [life=" + life + "]";
}
}
public class Girl {
@Override
public String toString() {
System.out.println("-----");
return super.toString();
}
//对象多态(类继承上)
public void play(Pet pet){
// 等价于:Pet pet = new Dog("旺财", "狗");
System.out.println("play with "+pet.name+"...");
//传狗的时候,bark
//instanceof 来判断该引用类型变量所“指向”的对象是否属于该类
if(pet instanceof Dog){
//向下转型
((Dog) pet).bark();
}
//传猫的时候,catchL
if(pet instanceof Cat){
//向下转型
((Cat) pet).catchL();
}
//传鸟时候,fly
if(pet instanceof Bird){
//向下转型
((Bird) pet).fly();
}
}
public static void main(String[] args) {
Dog dog = new Dog("旺财", "哈二");
Cat cat = new Cat("加菲猫", "肥猫");
Bird bird = new Bird("虎皮猫", "鹦鹉");
Girl girl = new Girl();
girl.play(bird);
girl.play(cat);
girl.play(dog);
//List<String> list = new ArrayList<String>();
}
}