面向对象三大特性和UML类图
Posted 惊天码盗
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了面向对象三大特性和UML类图相关的知识,希望对你有一定的参考价值。
所谓技术,就是用来解决现实问题的手段。
前言
今天主要了解一下面向对象的三大特性在JS中应用和怎么画一个简单的UML类图。
面向对象的三大特性
「 为什么要使用面向对象 」
class Persion {
constructor(name, age) {
this.name = name;
this.age = age;
}
eat() {
console.log(`${this.name} eat apple`);
}
speak() {
console.log(`My name is ${this.name} , age ${this.age}`);
}
}
let zhang = new Persion("zhang", 20);
zhang.speak();
//My name is zhang , age 20
let wang = new Persion("wang", 29);
wang.speak();
//My name is wang , age 29
接下来是面向对象的三大特性:继承、封装、多态。可能在业务中不常用,但是你必须知道。
「 继承」
class Student extends Persion {
constructor(name, age, gender) {
super(name, age);
}
}
let zhang = new Student("zhang", 20);
zhang.speak();
//My name is zhang , age 20
let wang = new Student("wang", 29);
wang.speak();
//My name is wang , age 29
「 封装」
public:完全开放
protected:对子类开放
private:对自己开放
这三个关键字ES6目前还不支持,我们用Ts来示范。
class Persion {
name; //默认是public
public age;
protected weight;
constructor(name, age) {
this.name = name;
this.age = age;
this.weight = 200;
}
speak() {
console.log(`My name is ${this.name} , age ${this.age}`);
}
}
class Student extends Persion {
number;
private friend; //外部取不到
constructor(name, age, number) {
super(name, age);
this.friend = "Tom";
}
getWeight() {
console.log(`weight is ${this.weight}`);
}
}
let zhang = new Student("zhang", 20);
zhang.getWeight(); //可以取到父类的weight
console.log(zhang.name); //可以取到
console.log(zhang.weight); //外部取不到
//Property 'weight' is protected and only accessible within class 'Persion' and its subclasses.
console.log(zhang.friend); //外部取不到
//Property 'friend' is private and only accessible within class 'Student'.
「 多态」
class Persion {
constructor(name, age) {
this.name = name;
this.age = age;
this.weight = 200;
}
speak() {
console.log(`My name is ${this.name} , age ${this.age}`);
}
}
class StudentA extends Persion {
constructor(name, age) {
super(name, age);
}
speak() {
console.log(`I am StudentA`);
}
}
class StudentB extends Persion {
constructor(name, age) {
super(name, age);
}
speak() {
console.log(`I am StudentB`);
}
}
let zhang = new StudentA("zhang", 20);
zhang.speak(); //I am StudentA
let wang = new StudentB("wang", 89);
wang.speak(); //I am StudentB
UML类图
那么我们就根据代码画一个类图,来表明类之间的关系。
class Persion {
constructor(name, house) {
this.name = name;
this.house = house;
}
speak() {
this.house.showCity(this.name);
}
}
class House {
constructor(city) {
this.city = city;
}
showCity(name) {
console.log(`I am ${name},My house is in ${this.city}`);
}
}
class StudentA extends Persion {
constructor(name, house, weight) {
super(name, house);
this.weight = weight;
}
speak() {
// console.log(`I am StudentA`);
this.house.showCity(this.name);
}
}
class StudentB extends Persion {
constructor(name, house, number) {
super(name, house);
this.number = number;
}
speak() {
// console.log(`I am StudentB`);
this.house.showCity(this.name);
}
}
let house = new House("beijing");
let zhang = new StudentA("zhang", house, "90");
zhang.speak();
//I am zhang,My house is in beijing
let wang = new StudentB("wang", house, 20);
wang.speak();
//I am wang,My house is in beijing
以上是关于面向对象三大特性和UML类图的主要内容,如果未能解决你的问题,请参考以下文章