Java怎样实现菱形继承
答案:2 悬赏:30 手机版
解决时间 2021-02-28 07:32
- 提问者网友:谁的错
- 2021-02-27 21:18
Java怎样实现菱形继承
最佳答案
- 五星知识达人网友:不想翻身的咸鱼
- 2021-02-27 22:20
首先要有个表示People行为的接口:
public interface PeopleBehavior {
void sayName();
}
则有People类实现如下:
public class People implements PeopleBehavior {
private final String mName;
public People(final String name) {
mName = name;
}
@Override
public void sayName() {
System.out.println("People:" + "sayName:" + mName);
}
}
为了模拟C++的虚继承两边指向同一个虚基类对象,参考Decorator pattern的设计,关键在于要有一个People类的wrapper:
public class PeopleWrapper implements PeopleBehavior {
private final People mPeople;
public PeopleWrapper(final People people) {
mPeople = people;
}
@Override
public void sayName() {
mPeople.sayName();
}
}
表示Programmer行为的接口:
public interface ProgramBehavior {
void setLanguage(final String lanaguage);
void program();
}
则有Programmer类:
public class Programmer extends PeopleWrapper implements ProgramBehavior {
private String mLanguage;
public Programmer(final People people) {
super(people);
}
@Override
public void program() {
System.out.println("Programmer:" + "program:" + mLanguage);
}
@Override
public void setLanguage(final String lanaguage) {
mLanguage = lanaguage;
}
}
同理表示Manager行为的接口:
public interface ManagerBehavior {
void setStyle(final String style);
void manage();
}
Manager类:
public class Manager extends PeopleWrapper implements ManagerBehavior {
private String mStyle;
public Manager(final People people) {
super(people);
}
@Override
public void setStyle(final String style) {
mStyle = style;
}
@Override
public void manage() {
System.out.println("Manager:" + "manage:" + mStyle);
}
}
如此,不难得出ProgramManager类的设计:
public class ProgramManager extends PeopleWrapper implements ProgramBehavior,
ManagerBehavior {
private final ProgramBehavior mProgramAbility;
private final ManagerBehavior mManagerAbility;
public ProgramManager(final People people) {
super(people);
mProgramAbility = new Programmer(people);
mManagerAbility = new Manager(people);
}
@Override
public void setStyle(final String style) {
mManagerAbility.setStyle(style);
}
@Override
public void manage() {
mManagerAbility.manage();
}
@Override
public void setLanguage(final String lanaguage) {
mProgramAbility.setLanguage(lanaguage);
}
@Override
public void program() {
mProgramAbility.program();
}
}
public interface PeopleBehavior {
void sayName();
}
则有People类实现如下:
public class People implements PeopleBehavior {
private final String mName;
public People(final String name) {
mName = name;
}
@Override
public void sayName() {
System.out.println("People:" + "sayName:" + mName);
}
}
为了模拟C++的虚继承两边指向同一个虚基类对象,参考Decorator pattern的设计,关键在于要有一个People类的wrapper:
public class PeopleWrapper implements PeopleBehavior {
private final People mPeople;
public PeopleWrapper(final People people) {
mPeople = people;
}
@Override
public void sayName() {
mPeople.sayName();
}
}
表示Programmer行为的接口:
public interface ProgramBehavior {
void setLanguage(final String lanaguage);
void program();
}
则有Programmer类:
public class Programmer extends PeopleWrapper implements ProgramBehavior {
private String mLanguage;
public Programmer(final People people) {
super(people);
}
@Override
public void program() {
System.out.println("Programmer:" + "program:" + mLanguage);
}
@Override
public void setLanguage(final String lanaguage) {
mLanguage = lanaguage;
}
}
同理表示Manager行为的接口:
public interface ManagerBehavior {
void setStyle(final String style);
void manage();
}
Manager类:
public class Manager extends PeopleWrapper implements ManagerBehavior {
private String mStyle;
public Manager(final People people) {
super(people);
}
@Override
public void setStyle(final String style) {
mStyle = style;
}
@Override
public void manage() {
System.out.println("Manager:" + "manage:" + mStyle);
}
}
如此,不难得出ProgramManager类的设计:
public class ProgramManager extends PeopleWrapper implements ProgramBehavior,
ManagerBehavior {
private final ProgramBehavior mProgramAbility;
private final ManagerBehavior mManagerAbility;
public ProgramManager(final People people) {
super(people);
mProgramAbility = new Programmer(people);
mManagerAbility = new Manager(people);
}
@Override
public void setStyle(final String style) {
mManagerAbility.setStyle(style);
}
@Override
public void manage() {
mManagerAbility.manage();
}
@Override
public void setLanguage(final String lanaguage) {
mProgramAbility.setLanguage(lanaguage);
}
@Override
public void program() {
mProgramAbility.program();
}
}
全部回答
- 1楼网友:慢性怪人
- 2021-02-27 22:59
public static void main(string[]args){
string s1=joptionpane.showinputdialog("请输入★单数★行");
int n=integer.parseint(s1);
for(int i=1;i<=(n+1)/2;i++){
for(int j=1;j<=(n+1)/2-i;j++){system.out.print(" ");}
for(int k=1;k<=2*i-1;k++){system.out.print("*");}
system.out.println();
}
for(int i=1;i<=(n-1)/2;i++){
for(int j=1;j<=i;j++){system.out.print(" ");}
for(int k=1;k<=n-2*i;k++){system.out.print("*");}
system.out.println();
}
}
???????的地方应该是 k<=n-2*i
下面的第i行有 n-2*i个 * 号
第2个问题没有看懂
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯