Section 2 Scala in object-oriented programming: 7, as well as the concept of inheritance and override the super keyword; 8, isInstanceOf keywords and asInstanceOf

6.3. Scala's inheritance object-oriented programming

6.3.1. Scala inherit (extends) the concept of

  • Scala, let subclass inherits the parent class, and Java, but also extends the use of keywords;
  • Represents inheritance, subclasses inherit field and method of the parent class, then subclasses can also be implemented in its own internal no parent class, and subclass specific field method, the use of inheritance can be efficiently multiplexed codes;
  • Subclass can override the field and method of the parent class, but if the parent with a final modification, or modified by the final field and method, then the class can not be inherited, or field and method are not covered.
  • private field and modified method can not be inherited by subclasses, it can only be used within the class;
  • field must be defined as a form of val can be inherited, and also to use the override keyword. Because var modified field is variable, in a subclass can be directly reference is assigned, need not be inherited; val i.e. modified allowed to be inherited, and may only be modified var reference. Inheritance is to change the cover means. (Note: the parent class and subclass should use val modified)
  • Access control permissions in Java, the same applies to Scala

 

Inner classes

This package

Subclass

External packaging

public

protected

×

default

×

×

private

×

×

×

 

  • for example:
Package cn.itcast.extends_demo class of Person1 { Val name = "Super" DEF getName = the this . name } class student1 the extends of Person1 { 
  // add the keyword inherit the override 
  Val name = "Sub" 
  // Subclasses can define their own field and Method Val Score = "A" DEF getScore = the this . Score }


 
 



 


 
 

 

6.3.2. Scala in override and super keywords

  • Scala, a non-abstract subclass if the parent class's method to be covered, you must use the override keyword; subclass can override the parent class val modified field, just use the override keyword in a subclass can be.
  • override keywords can help developers as soon as possible find errors in your code, for example, modifying the method name override the parent class method of spelling errors.
  • In addition, after the subclass override the parent class method in a subclass if you want to call the parent class's method is covered, you must use the super keyword, it pointed out that the parent class method to call display.
  • for example:
Package cn.itcast.extends_demo class the Person2 { Private Val name = "LEO" Val Age = 50 DEF getName = the this . name } class STUDENT2 the extends the Person2 { Private Val Score = "A" 
  // Subclasses can override the parent val field , use the override keyword override 
  Val Age = 30 DEF getScore = the this . Score non-abstract parent class method // coverage, we must use the override keyword 
  // parent class method calls at the same time, the use of super keyword override DEF getName = "your IS name "+ Super .getName


 
 
 


 

 

 
 


  
}

6.3.3. Scala in isInstanceOf and asInstanceOf

If an instance of an object subclass, but it gives the parent class types of variables in the subsequent process, they need to convert the parent class is a subclass of the type of a variable type of variable, what should you do?

Class A extends class B

B b=new A

 

  • First, the need to use the target object is determined whether isInstanceOf specified class, and if so, can be used to convert the object asInstanceOf specified type;
  • Note : p.isInstanceOf [XX] Examples of p determines whether the object XX; p.asInstanceOf [XX] XX is converted to the p instances of the object
  • Note : If you do not use isInstanceOf first determine whether the object is an instance of the specified class, the direct use of asInstanceOf conversion, you may throw an exception;
  • Note : If the object is null, the isInstanceOf must return false, asInstanceOf must return null;
  • Scala and Java type checking and conversion

 

Scala

Java

obj.isInstanceOf[C]

obj instanceof C

obj.asInstanceOf[C]

(C)obj

classOf[C]

C.class

 

  • for example:
Package cn.itcast.extends_demo class person3 {} class Student3 the extends person3 Object Student3 { DEF main (args: the Array [String]) { Val P: = person3 new new Student3 var S: Student3 = null // If the object is null, then isInstanceOf must return to false the println (s.isInstanceOf [Student3]) 
    // p is determined whether an object instance is Student3 iF (p.isInstanceOf [Student3]) { 
    // converts into Student3 object instance p 
      s = p.asInstanceOf [Student3] 
  } the println (s.isInstanceOf [Student3]) 
  } 
}




   
   
   
   

   

 



 

 

 

 

Guess you like

Origin www.cnblogs.com/mediocreWorld/p/11361390.html