(C #, JavaScript) Object-oriented programming

Object-oriented (OOP) understanding

Friends who like the program, everyone should have heard the sentence " Everything is an object ", feeling old X.

Object-oriented programming, it is designed around the real world.

Object-oriented three elements: encapsulation, inheritance, and polymorphism.

We can define a class to represent the car, and define some attributes and behavior of the car. However, in reality, there are many types of cars, such as buses, trucks, and taxis. Is it necessary to define the corresponding class every time? In this case, it is too much trouble, we can abstract these once, whether these cars belong to the category of cars. In this case, there are many attributes and behaviors that everyone has, but they are different. Then we define an abstract class (Vehicle), let buses, trucks, and taxis inherit this class, and then only need to rewrite it Method, or modify the corresponding properties. After the above work is done, you can instantiate the bus, truck, and taxi (that is, the new operation). Only after the instantiation can you be called an object. From this, everything is an object, so naturally understand it.
(If you do n’t have a girlfriend, let ’s get a new one, hehehe)

Relieve eye fatigue

There are two main types of object-oriented languages: 1. Strongly typed (C #, Java, etc.) 2. Weakly typed (JavaScript).

1. First, use the strongly typed C # language to specifically introduce object-oriented. (Java personally does not know too much ...) The
code is as follows:
encapsulation

//定义一个抽象类 Vehicle 将一些属性跟行为封装在这个类里供后面的类继承
abstract class Vehicle()
{
    public string myName;//这个是字段,不是属性

    //属性
    public string Appearance { get; set; } = "red"; //外观颜色,默认是红色,而且该属性可读可写
    public string Weight { get; set; } = "1.8t" //车身重量
    public string Brand { get; set; } //品牌
    //行为(方法)
    public virtual void Run() //使用virtual关键字以供继承它的类重写该方法
    {
        //启动
    }
    public virtual void Stop()
    {
        //停车
    }
}

inherit

class Car : Vehicle()//小轿车继承Vehicle这个类
{
    //属性重新赋值---构造函数(用来构造对象的,顺便设置本类的部分属性值)
    public Car()
    {
        this.Appearance = "小轿车外观";
        this.Weight = "1.5t";
    }
    //行为(方法)  重写Vehicle里面的Run方法
    public override void Run()
    {
        //小轿车启动
    }
    public override void Stop()
    {
        //小轿车停车
    }
}
class Bus : Vehicle()
{
    //属性
    public Bus()
    {
        this.Appearance = "公交车外观";
        this.Weight = "2t";
    }
    //行为(方法)
    public override void Run()
    {
        //公交车启动
    }
    public override void Stop()
    {
        //公交车停车
    }
}

Polymorphism

class static void Main()//程序入口
{
    Car car = new Car();//实例化一个对象
    car.Brand = "劳斯莱斯";//重新设置属性的值
    car.Run();//启动小轿车
    Bus bus = new Bus();
    bus.Run();//启动公交车
    //小轿车和公交车调用了同一个Run方法,却产生了不同的效果;
    这便是多态,不同的对象调用调用同一个方法,产生了不同的行为。也是面向对象程序设计的魅力所在
}

2. Next, use the weakly typed JavaScript language to introduce object-oriented.
code show as below:

//最简单的方式
var Vehicle={
    Appearance:"",
    Weight:"",
    Brand:"",
    Run:function(){},
    Stop:function(){}
}
//js语言也可以设置某个属性是可读还是可写,只不过稍微有些复杂
如下:
Objec.defineProperty(Vehicle,"Appearance",{
    writable: false//不可写,意思就是不能修改该属性的值
})
//组合使用构造函数模式跟原型模式---这里可能有些复杂,具体原型在之后的博客中详细介绍
function Vehicle(Appearance,Weight,Brand)
{
    this.Appearance=Appearance;
    this.Weight=Weight;
    this.Brand=Brand;
}
Vehicle.prototype={
    constructor:Vehicle,
    Run:function(){},
    Stop:function(){}
}

See here to explain the difference between strong type and weak type:
strong type: refers to the specific value type is certain, the definition of int type variable, you need to use the int keyword; the definition of string type variable, you need to use string Keywords; now C # also supports the use of the var keyword to define variables, but when defining, you must assign values. The principle is actually to guess what type the variable is based on the assigned value, that is to say, based on the assigned value to guess whether the var refers to int or string.

Weakly typed: In JavaScript, all variables are defined using the var keyword, no matter what type of variable can be defined using var.

Relieve eye fatigue 2

By the way, C # (Java) also belongs to the ranks of server-side languages, and JavaScript belongs to the client-side language.
What do you mean?
That is, C # runs on the server, and JavaScript runs on the client. The JavaScript language in the web pages that we usually browse is running on the browser. We can directly see the JavaScript code through the browser, but we can't see the server-side language.

Guess you like

Origin www.cnblogs.com/ywjbokeyuan/p/12639451.html