"Class - Basic Concepts 2" Learned by Scala

Internal Class

import scala.collection.mutable.ArrayBuffer
class Class {
	class Student(val name: String) {}		//Internal class here
	val students = new ArrayBuffer[Student]
	def getStudent(name: String) = {
		new Student(name)
	}
}

val c1 = new Class
val s1 = c1.getStudent("0mifang")
c1.students += s1

val c2 = new Class
val s2 = c2.getStudent("0mifang1")
c1.students += s2		//Error presentation

extends

  • Inheritance means that the child class can inherit the field and method of the parent class from the parent class; then the child class can put in its own interior a field and method unique to the child class that can effectively reuse code.
  • Subclasses can override the field and method of the parent class, but if the parent class is modified with final and the field and method are modified with final, the class cannot be inherited and the field and method cannot be overridden.
class Person {
	private var name = "0mifang"
	def getName = name
}
class Student extends Person {
	private var score = "A"
	def getScore = score
}

var student = new Student
student.getName

override and super

  • If a subclass overrides a non-abstract method in a parent class, the override keyword override must be used
  • Keyword can help us get rid of errors in code as soon as possible, such as: the method name of override-modified parent method is misspelled; the parameter of parent method to be overridden is misspelled; etc.
  • In addition, after the parent method is overridden by the subclass, if we are calling the overridden method of the parent class in the subclass, then we can explicitly specify the method to call the parent class using the super keyword
class Person {
	private var name = "0mifang"
	def getName = name

}
class Student extends Person {
	private var score = "A"
	def getScore = score

	override def getName = "Hi, I'm " + super.getName
}

var student = new Student
student.getName		// You can see that the student class inherits the getName method of the Person class

override field

The subclass can override the val field of the parent, and the val field of the subclass can override the getter method of the val field of the parent as long as the override keyword is used in the subclass

class Person {
	val name: String = "Person"
	def age: Int = 0
}

class Student extends Person {
	override val name: String = "0mifang"
	override val age: Int = 18
}

val person = new Person
val student = new Student
person.name
person.age
student.name
student.age

isInstanceOf and asInstanceOf

If we create an object of a subclass but assign it to a variable of the parent type.In subsequent programs, we need to convert variables of the parent type into variables of the child type. What should we do? First, you need to use isInstanceOf to determine if the object is an object of the specified class, and if so, you can use asInstanceOf to convert the object to the specified type.

  • If the object is null, isInstanceOf must return false and asInstanceOf must return null.
  • If the isInstanceOf is not used to determine whether an object is an instance of a specified class, then the asInstanceOf conversion will throw an exception.
class Person
class Student extends Person
val p: Person =  new Student
var s: Student = null
// If p is a Student class, let s point to the object p converts to Student
if (p.isInstanceOf[Student]) s = p.asInstanceOf[Student]

getClass and classOf

  • isInstanceOf can only judge whether an object is an object of a specified class and its subclasses, but cannot accurately judge whether an object is an object of a specified class.
  • Object.getClass can get the class of the object precisely, classOf [class] can get the class precisely, and then use the==operator to judge.
class Person
class Student extends Person

val p: Person = new Student
p.isInstanceOf[Person]		//true
p.getClass == classOf[Person]	//false
p.getClass == classOf[Student]	//true

Welcome, this number will continue to share my various experiences on the programming road.

Keywords: Programming Scala

Added by zzz on Wed, 22 Jan 2020 06:12:18 +0200