Declaration of functions
Function overloading
Class declarative use and inheritance
Static properties and methods
Access control
The so-called encapsulation in object-oriented is essentially to gather a series of related data (attributes/variables/fields) and methods of manipulating data (methods/functions) into a data structure (classes), so as to hide data and methods of manipulating data and expose limited methods of manipulating data externally.
In TypeScript, access control characters are used to protect access to classes, variables, methods, and constructors. TypeScript supports three different access rights. \
- Public (default) : Public and can be accessed anywhere.
- Protected: It is protected and can be accessed by itself as well as by its subclasses and superclasses.
- Private: private, accessible only by the class in which it is defined.
/ Public property: allows calls from anywhere// Private attributes: only calls are allowed from within the current class
// Protect property: only allow calls from within the current class or subclasses that directly or indirectly inherit from the current class
class Proto{
public desc(){ // Public method
return I live in a tree; }}class Humen extends Proto{
public address:string = "Beijing"; // Public attributes
public desc(){ // Public method
return I live in `The ${this.address}`;
}
private money:number = 10000; // Private attributes
private calc_money(){
return this.money*0.1; Private attributes, private methods can only be called inside a class
}
// If private attributes are allowed to be exposed to the outside world, they are usually exposed through public methods
public show_money(){
return this.calc_money();
}
protected phone:string = "13300000000"; // Protect properties
protected get_phone(){ // Protection method
return 'My mobile phone number:The ${this.phone}`; // Only inside a class or a subclass can call protected properties/methods
}
// If protected attributes are allowed to be viewed by the outside world, they are usually exposed through public methods
public show_phone(key?){
if(key == "123456") {return this.get_phone(); }}}class People extends Humen{
public show_father_data(){
// return this.phone; // The protection attribute of the parent class is called
// return this.get_phone(); // The protection method of the parent class is called
return this.show_money(); // Subclasses cannot call private attributes or methods of their parent class
// return this.desc(); // Calls the inherited parent method or property, overridden if the current class is overloaded
// return super.desc();
}
public desc(){
return 'Hello, I live inThe ${this.address}`; }}var xiaoming = new People();
// console.log(xiaoming.phone); Private or protected attributes cannot be called outside the class
// console.log(xiaoming.address);
// console.log(xiaoming.desc());
// console.log(xiaoming.show_money());
// console.log(xiaoming.show_phone());
// console.log(xiaoming.show_phone(123456));
console.log(xiaoming.show_father_data());
Copy the code
accessor
An abstract class
Abstract classes are used as base classes for other derived classes. They are generally not instantiated directly. Unlike interfaces, abstract classes can contain implementation details of members. The abstract keyword is used to define abstract classes and abstract methods within abstract classes.
Code:
// Abstract superclass
abstract class Animal{
abstract makeSound(): void; // Abstract method, no function body
desc(): void { // Public methods or public properties of subclasses can also be defined in abstract classes
console.log('roaming the earch... '); }}// Abstract superclass
abstract class Dog extends Animal{
abstract nickname:string;
abstract move(): string;
}
// Concrete class
class ChineseGardenDog extends Dog{
public nickname:string;
constructor(nickname:string){
super(a);// A subclass of an abstract class must be initialized
this.nickname = nickname;
}
makeSound(){
return "Woof woof woof."
}
move(): string {
return "On the run...."; }}var dog = new ChineseGardenDog("Live");
console.log(dog.nickname);
console.log(dog.makeSound());
Copy the code
Abstract classes in Python
There are no abstract classes and interfaces in python’s basic syntax. So, to implement scenarios like interfaces or abstract classes, you can use python’s built-in standard library, the ABC module.
import abc Implement abstract classes using the ABC module
class File(metaclass=abc.ABCMeta) : # abc.abcMeta is a base class that implements abstract classes
@abc.abstractmethod Define abstract methods without implementing functionality
def read(self) :
pass
class Txt(File) : Subclasses inherit abstract classes, but must define a read method to override the read method in the abstract class
def read(self) :
print('Method of reading text data')
if __name__ == '__main__':
txt1 = Txt()
txt1.read()
Abstract classes cannot be instantiated directly
tet2 = File()
tet2.read()
Copy the code