gpt4 book ai didi

javascript - 如何在 JavaScript 中将功能放入抽象函数中

转载 作者:行者123 更新时间:2023-11-27 22:57:28 24 4
gpt4 key购买 nike

我试图让 JavaScript 对象中的一些方法可以由子类继承,但我不想允许父类被实例化。这是我编写的一些代码来说明这一点:

/**
* Shows how basic abstraction works with JavaScript
*/

//Define the person object with a first name, last name, and an age
function Person(firstName, lastName, age) {
//Make it so that this object cannot be instantiated by identifying its constructor
if(this.constructor === Person) {
throw new Error("Can't instantiate an abstract class of type Person!");
}

//Assign instance variables
this.firstName = firstName;
this.lastName = lastName;
this.age = age;

//Create simple get methods
this.getName = function(){
return this.firstName + " " + this.lastName;
}

this.getFirstName = function() {
return this.firstName;
}

this.getLastName = function() {
return this.lastName;
}

this.getAge = function() {
return this.age;
}
}

//Define the student constructor
function Student(firstName, lastName, age, subject) {
//Assign the instance variables including the new subject variable
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.subject = subject;

//Add a new function to get the subject from Student
this.getSubject = function() {
return this.subject;
}
}
Student.prototype = Object.create(Person.prototype);
Student.prototype.constructor = Student;

//Testing the inheritance
var joe = new Student("Joe", "Shmo", 33, "Computer Science");
console.log("First Name: " + joe.getFirstName()); //The getFirstName() function is defined in the superclass
console.log("Subject: " + joe.getSubject()); //The getSubject() function is defined in the subclass

使用此代码,我在尝试对 Student 对象 joe 调用 getFirstName 时收到错误。看来让子类继承 getFirstName 会非常有用。

我真的希望能够在父类中定义 getName 函数,这样我就可以让子类(例如 Student)继承该功能。有什么办法可以做到这一点吗?我真的很感激任何帮助!

最佳答案

您需要在 Person 原型(prototype)中定义方法,而不是在 Person 的实例中。这样,当您执行 Object.create(Person.prototype) 时,它们将被复制:

/**
* Shows how basic abstraction works with JavaScript
*/

//Define the person object with a first name, last name, and an age
function Person(firstName, lastName, age) {
//Make it so that this object cannot be instantiated by identifying its constructor
if(this.constructor === Person) {
throw new Error("Can't instantiate an abstract class of type Person!");
}

//Assign instance variables
this.firstName = firstName;
this.lastName = lastName;
this.age = age;

}

Person.prototype.getName = function(){
return this.firstName + " " + this.lastName;
}

Person.prototype.getFirstName = function() {
return this.firstName;
}

Person.prototype.getLastName = function() {
return this.lastName;
}

Person.prototype.getAge = function() {
return this.age;
}

//Define the student constructor
function Student(firstName, lastName, age, subject) {
//Assign the instance variables including the new subject variable
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.subject = subject;

//Add a new function to get the subject from Student
this.getSubject = function() {
return this.subject;
}
}
Student.prototype = Object.create(Person.prototype);
Student.prototype.constructor = Student;

//Testing the inheritance
var joe = new Student("Joe", "Shmo", 33, "Computer Science");
console.log("First Name: " + joe.getFirstName()); //The getFirstName() function is defined in the superclass
console.log("Subject: " + joe.getSubject()); //The getSubject() function is defined in the subclass

关于javascript - 如何在 JavaScript 中将功能放入抽象函数中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37466627/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com