【问题标题】:Passing object into one function, and using the key/values from that object to call another function将对象传递给一个函数,并使用该对象的键/值来调用另一个函数
【发布时间】:2018-04-27 01:03:31
【问题描述】:

在python中我通常只是做这样的事情:

## single person
class Person:
    def __init__(self, first_name, last_name, age):
        self.first_name = first_name
        self.last_name = last_name
        self.age = age

## empty when instantiated
class People:
    def __init_(self):
        self._people = {}

    def update(self, Person):
        self._people[Person.first_name] = Person

    def update2(self, person):
        self.update(Person(**person))


people = People()
people.update2({'first_name': 'Mike', 'last_name': 'Smith', 'age':7})

我的目标是在 typescript 中实现这种精确的行为。这是我到目前为止所拥有的。

class  Person {
    constructor(first_name, last_name, age){}
}

class People{
    public _people;

    constructor(){
        //not sure if there is a cleaner way to add _people to new instances
        this._people = {}
    }

    update(Person){
        this._people[Person.first_name] = Person
    }
    update2(my_object){
        //Person(my_object) should return an instance of the Person class
        this.update(Person(my_object))
    }
}

var people = new People()
people.update2({first_name:'Bob', last_name:'Smith', age:7})

非python人士的解释。

目标是创建一个可以保存 Person 类实例的 People 类。我想将一个对象传递给 update2,并使用该对象的键/值来创建 Person 类的实例。

如果有任何不清楚的地方,请告诉我。

【问题讨论】:

    标签: typescript


    【解决方案1】:

    这是我用 TypeScript 编写它的方式。我已经更改了一些变量的大小写,使其更像“TypeScript”。

    我还添加了缺少的类型。

    class Person {
        // Public getters/setters
        public firstName: string;
        public lastName: string;
        public age: number;
    
        constructor({firstName, lastName, age}: { firstName?: string, lastName?: string, age?: number }) {
            this.firstName = firstName;
            this.lastName = lastName;
            this.age = age;
        }
    }
    
    class People {
        // Declare _people as an object with keys as strings and values as Person instances
        public _people: {[key: string]: Person};
    
        update(person: Person) {
            this._people[person.firstName] = person;
        }
    
        // Add a signature to obj to make it clear what you are expecting
        update2(obj: {firstName: string, lastName: string, age: number}) {
            this.update(new Person(obj));
        }
    }
    
    var people = new People()
    people.update2({ firstName: 'Bob', lastName: 'Smith', age: 7 });
    
    console.log(people._people);
    

    或者如果Person是一个简单的数据对象,我建议你不要使用class,而是一个带有接口的简单JS对象:

    interface Person {
        firstName?: string,
        lastName?: string,
        age?: number
    }
    
    const person1: Person = { firstName: 'Jane', lastName: 'Doe', age: 25};
    

    【讨论】:

    • 几个问题。在 Person 中,构造函数中的代码不是自动完成的,不需要写出来吗?在 People 中,_people 会在所有人员实例之间共享吗?我希望 People 的每个新实例都有自己的 _people 对象。此外,在许多情况下,我的课程有很多属性。无论如何要避免在update2中写入obj.firstName,obj.lastName ...等?谢谢,这可能正是我正在寻找的。​​span>
    • 编辑了我的答案。不,据我所知,您需要在构造函数中设置属性。 _people 不会在实例之间共享(就像我知道的任何 OO 语言一样)。为了避免编写 obj.firstName 等,您可以使用我现在展示的对象解构。如果 Person 是一个简单的数据对象,直接使用一个对象,而不是一个类。
    • stackoverflow.com/questions/33217963/… (解构看起来你需要设置它们,否则不需要)。在 python 中,类变量在实例之间共享。 Person会有很多方法,在实际执行中。感谢您的 sn-p,应该让我到达我需要去的地方。
    猜你喜欢
    • 1970-01-01
    • 2017-06-03
    • 2018-11-28
    • 1970-01-01
    • 2017-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多