Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
面向对象Javascript-如何在类中定义类?从一个C#示例_Javascript_Oop - Fatal编程技术网

面向对象Javascript-如何在类中定义类?从一个C#示例

面向对象Javascript-如何在类中定义类?从一个C#示例,javascript,oop,Javascript,Oop,我知道有很多关于OOJavaScript的问题,所以我已经阅读了很多参考资料。。。。但这仍然是我迄今为止最冗长的学习曲线 对不起,我不是受过古典训练的,所以我只能给你们看c#,这是我想要实现的一个例子 我希望你能帮忙 public class Engine { public int EngineSize; public Engine() { } } public class Car { public Engine engine; public

我知道有很多关于OOJavaScript的问题,所以我已经阅读了很多参考资料。。。。但这仍然是我迄今为止最冗长的学习曲线

对不起,我不是受过古典训练的,所以我只能给你们看c#,这是我想要实现的一个例子

我希望你能帮忙

public class Engine
{
    public int EngineSize;

    public Engine()
    {
    }
}
public class Car
{
    public Engine engine;

    public Car()
    {
        engine = new Engine();
    }
}
伙计们,我真的不担心上述C#示例中的私有/公共和命名约定

我只想知道如何在Javascript中复制这个结构


谢谢

那么你真的只想知道一个对象如何包含另一个对象?下面是您的示例的一个非常简单的转换:

function Engine()
{
    this.EngineSize=1600;
}

function Car()
{
    this.engine=new Engine();
}

var myCar=new Car();

你什么时候做新车。汽车构造师将创建引擎的新实例,并将其分配给汽车实例的引擎属性。

以下是ES6的答案:

function Engine(size) {
    var privateVar;

    function privateMethod () {
      //...
    }

    this.publicMethod = function () {
       // with access to private variables and methods
    };

    this.engineSize = size; // public 'field'
}

function Car() { // generic car
    this.engine = new Engine();
}

function BMW1800 () {
  this.engine =  new Engine(1800);
}

BMW1800.prototype = new Car(); // inherit from Car


var myCar = new BMW1800();
class Engine {
  // Constructor, size defaults to 1
  constructor(size = 1) {
    // Sets size to size
    this.size = size;
  }
  // Method
  start() {
    for (let i = 0; i < this.size, i++) {
      alert('Brrr');
    }
  }
}
class Car {
  // Constructor
  constructor(name) {
    // Sets engine to a new engine
    this.engine = new Engine();
    // Sets name to name
    this.name = name;
  }
  // Method
  introduceTo(name) {
    alert(`Hey ${name}, here is my car ${this.name}.`);
  }
}

let myCar = new Car('Ferrari');
myCar.engine = new Engine(5);
myCar.introduceTo('bro');
myCar.engine.start();
类引擎{
//构造函数,大小默认为1
构造函数(大小=1){
//将大小设置为大小
这个。大小=大小;
}
//方法
开始(){
for(设i=0;i

(我做了一些补充,希望你喜欢)

这应该是C#吗?
公共类之后的paren是什么…
?我比这里的其他示例更喜欢它,因为它使用javascript功能提供了一个从外部无法访问的私有名称空间。我也会在车上展示同样的东西,但至少你的privateVar演示了信息隐藏。
class Engine {
  // Constructor, size defaults to 1
  constructor(size = 1) {
    // Sets size to size
    this.size = size;
  }
  // Method
  start() {
    for (let i = 0; i < this.size, i++) {
      alert('Brrr');
    }
  }
}
class Car {
  // Constructor
  constructor(name) {
    // Sets engine to a new engine
    this.engine = new Engine();
    // Sets name to name
    this.name = name;
  }
  // Method
  introduceTo(name) {
    alert(`Hey ${name}, here is my car ${this.name}.`);
  }
}

let myCar = new Car('Ferrari');
myCar.engine = new Engine(5);
myCar.introduceTo('bro');
myCar.engine.start();