java学习第五天

final 关键字
            能够修饰类,属性,方法java

            一、修饰变量(成员变量或局部变量),则称为常量.只能赋值一次安全

            二、修饰方法,该方法不能被重写ide

            三、修饰类,则该类不能被继承this

--------------------------------------------------------------
final 修饰的类表示,这个类是最终类,是没法被继承的,也就是说它没有子类对象

运用场景:不想让其有子类的场景下使用(处于安全的考虑)继承

final 修饰方法 表示该方法是最终方法,没法被子类进行重写,可是能够继承ip

运用场景:不想让其有子类的修改父类核心功能
public /*final */class Father {
 //String
 public final void info(){
  System.out.println("Father.info()");
 }
 
 
}内存

-------------------------------------------------------------------------it

 


/*
 * final 修饰类:
 *  一、表示类是最终类,不能被继承
 *  二、运用场景:不想让其有子类的场景下使用(处于安全的考虑)
 *
 *
 * final 修饰属性
 *  一、声明的时候必须指定具体的
 *  二、基本类型,在指定具体的值后,没法修改
 *  三、引用类型,在指定具体的值后,没法修改,可是,能够修改引用对象的具体属性(非final)
 *  四、运用场景:在内部类使用
 *
 * final 修饰方法
 *  一、表示最终方法,不能被子类重写,能够继承
 *  二、运用场景:不想让其有子类的修改父类核心功能
 *
 * final 跟 static 配合使用
 *  一、表示常量
 *  二、运用场景: 系统参数的标识 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();
  
  
 }
}io


-----------------------------------------------------------------------

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("旺财", "狗");
 *  一、不须要强制转换
 *  二、能够访问父类的方法,属性
 *  三、没法访问子类特有的方法及属性(对子类方法或属性进行遮蔽)
 * 
 *  向下转型:
 *  一、须要强制转换
 *  二、若是把一个类型转换成其余类型,编译没有报错,执行报错:
 *     java.lang.ClassCastException   --类转换异常
 *  三、若是把一个类型转换成其余类型,而后再由其余类型转换会本类型,编译执行都经过
 */

-------------------------------------------------------------------

instaceof 和向上转型 和 向下转型 配合使用

instaceof 关键字  --> 用来判断该引用类型变量所"指向"的对象是否赋予该类

 


-----------------------------------------------------------------------------------------


一、父类
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 吃饭了。。。");
 }
}

二、子类
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("鸟 飞得更高。。。");
 }

三、子类
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)。。。");
 }
}


四、子类

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>();   }}

相关文章
相关标签/搜索