如何仅使用类的特定属性

时间:2018-03-09 06:27:03

标签: typescript object attributes

我的项目有一个患者模型,其中有许多属性。我在一个页面中使用少量属性,在另一个页面中使用少量其他属性。我有一个共同的类,我如何在另一个方法中只使用少数属性。

class Address {
constructor(
public addressLine1: String,
public addressLine2: String,
public city: String,
public state: String,
public zipcode: String
) { }
}

export class PatientModel {
constructor(
public firstName: String,
public lastName: String,
public dob: Date,
public addresses: Address,
public phone: String,
public validFlag: Boolean,
public medications: String,
public injuries: String,
public allergies: String,
public isSmoker: Boolean,
public isAlchoholic: Boolean,
public isAddict: Boolean
 ) { }

public static createBlank() {
let address = new Address('', '', '', '', '12345')
return new PatientModel('testfirst', 'testLast', new Date(1990, 4,4), 
address, '1234454343',  true,'','','',false,false,false);
}

public static medicalInfo(){
  // here i need only medications, allergies, injuries, smoker, alchoholic, 
     addict details..
}

}

1 个答案:

答案 0 :(得分:0)

在构造函数中使用optional or default parameters

例如,要使isAlchoholic成为可选项,isAddict具有默认值:

export class PatientModel {
    constructor(
        public firstName: String,
        public lastName: String,
        public dob: Date,
        public addresses: Address,
        public phone: String,
        public validFlag: Boolean,
        public medications: String,
        public injuries: String,
        public allergies: String,
        public isSmoker: Boolean,
        public isAlchoholic?: Boolean,
        public isAddict: Boolean = false
    ) { }
}

请注意,所有可选/默认参数必须遵循所有必需参数。在所需参数之间或之前,您不能有可选/默认参数。