我有以下课程:import { Component, OnInit } from '@angular/core';
import { ProjectService } from '../services/project.service';
import { Router, ActivatedRoute } from '@angular/router';
import { Location } from '@angular/common';
import { DomSanitizer } from '@angular/platform-browser';
import { environment } from '../../../../environments/environment';
@Component({
selector: 'app-project-details',
templateUrl: './project-details.component.html',
styleUrls: ['./project-details.component.css']
})
export class ProjectDetailsComponent implements OnInit {
project: any;
apiUrl: string;
project_id: string;
constructor(
private router: Router,
private activatedRoute: ActivatedRoute,
private projectService: ProjectService,
private _location: Location,
private sanitization: DomSanitizer
) {
this.apiUrl = environment.apiUrl;
this.activatedRoute.params
.subscribe(params => {
console.log(params.project_id)
this.project_id = params.project_id;
this.getProjectByID(params.project_id);
})
}
ngOnInit() {
}
getProjectByID(project_id: string) {
this.projectService.getProjectById(project_id).subscribe((data: any) => {
data.project.projectImages.map(image => {
image.path = this.sanitization.bypassSecurityTrustUrl(`${this.apiUrl}/${image.path}`);
return image;
});
this.project = data.project;
}, err => {
console.log(err);
});
}
Item是父类,Swords和food继承了Item类。我想为这些类中的每一个提供一堆不同的功能和描述。
我完全了解事物的功能方面,但是我在努力寻找如何用子类覆盖默认描述的方法。
我目前在SQLAlchemy中使用单表继承。
答案 0 :(得分:1)
信贷请Mike Bayer帮助我弄清楚如何改写__init__()
以适合我的需要。如果有人能找到更优雅的解决方案,我将很乐意接受该答案。
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String, default="User")
fullname = Column(String)
email = Column(String)
type = Column(String)
__mapper_args__ = {
'polymorphic_on': type,
'polymorphic_identity': 'user'
}
def __init__(self, **kwargs):
super().__init__(**kwargs)
def __repr__(self):
return f"<User(id={self.id}, name='{self.name}', fullname='{self.fullname}', email='{self.email}')>"
def is_admin(self):
print(f"I, {self.name}, am NOT an admin")
class Admin(User):
def __init__(self, **kwargs):
kwargs.setdefault("name", "Admin")
super().__init__(**kwargs)
__mapper_args__ = {
'polymorphic_identity': 'admin'
}