第三周命题作业

发布于: 2020 年 06 月 24 日

作业1

作业2

public interface Component {
void print();
String getName();
void setName(String name);
}
import java.util.List;
public interface ComponentRegistry {
void addComponent(Component component);
void removeComponent(Component component);
boolean exists(Component component);
List<Component> getComponents();
}
public class GenericComponent implements Component {
private String name;
@Override
public void print() {
System.out.println("Print " + getName());
}
@Override
public String getName() {
return this.name;
}
@Override
public void setName(String name) {
this.name = name;
}
}
import java.util.ArrayList;
import java.util.List;
public class ContainerComponent extends GenericComponent implements ComponentRegistry{
private List<Component> components;
public ContainerComponent() {
components = new ArrayList<>();
}
@Override
public void addComponent(Component component) {
this.components.add(component);
}
@Override
public void removeComponent(Component component) {
this.components.remove(component);
}
@Override
public boolean exists(Component component) {
return this.components.contains(component);
}
@Override
public List<Component> getComponents() {
return this.components;
}
}
public class Frame extends ContainerComponent{
public Frame() {
setName("Frame");
}
}
public class Label extends GenericComponent{
public Label() {
setName("Label");
}
}
public class LinkLabel extends GenericComponent{
public LinkLabel() {
setName("LinkLabel");
}
}
public class PasswordBox extends GenericComponent{
public PasswordBox() {
setName("PasswordBox");
}
}
public class Picture extends GenericComponent{
public Picture() {
setName("Picture");
}
}
public class TextBox extends GenericComponent{
public TextBox() {
setName("TextBox");
}
}
public class Window extends ContainerComponent{
public Window() {
setName("Window");
}
}
public class CheckBox extends GenericComponent{
public CheckBox() {
setName("CheckBox");
}
}
public class Button extends GenericComponent{
public Button() {
setName("Button");
}
}
import java.util.LinkedList;
import java.util.Queue;
public class ComponentTest {
public static void main(String[] args) {
ContainerComponent window = new Window();
window.addComponent(new Picture());
window.addComponent(new Button());
window.addComponent(new Button());
ContainerComponent frame = new Frame();
window.addComponent(frame);
frame.addComponent(new Label());
frame.addComponent(new TextBox());
frame.addComponent(new Label());
frame.addComponent(new PasswordBox());
frame.addComponent(new CheckBox());
frame.addComponent(new TextBox());
frame.addComponent(new LinkLabel());
print(window);
}
private static void print(ContainerComponent window) {
window.print();
Queue<Component> queue = new LinkedList<Component>();
queue.offer(window);
while(!queue.isEmpty()){
Component currentComponent = queue.poll();
if(currentComponent instanceof ContainerComponent) {
for(Component component : ((ContainerComponent) currentComponent).getComponents()) {
component.print();
queue.offer(component);
}
}
}
}

为了凑够50个字

为了凑够50个字

为了凑够50个字

为了凑够50个字

为了凑够50个字

为了凑够50个字

为了凑够50个字

为了凑够50个字

为了凑够50个字

为了凑够50个字

用户头像

Geek_a327d3

关注

还未添加个人签名 2020.04.14 加入

还未添加个人简介

评论

发布
暂无评论
第三周命题作业