- 学习内容
- 1.占位符T
- 2.树形结构的构造
- 工作内容
- java任务
- codegym任务
- 课程1
- 任务1 整只鸭子还不够
- 任务2 男人和女人
单独的T就是表示限制你传递的参数类型。
2.树形结构的构造只构造了树形结构,还没有遍历。
可以说的内容基本都卸载代码的注释里了
代码如下:
package com.xxm.exam1;
import java.util.ArrayList;
import java.util.List;
public class task29 {
public class Node {
//定义Note类
private T data = null;
//定义并初始化
private List> children = new ArrayList<>();
//声明并初始化Node类的子节点
private Node parent = null;
//声明并初始化Node类的父节点
public Node(T data) {
this.data = data;
}
//定义Node类数据的设置方法
public Node addChild(Node child) {
child.setParent(this);
this.children.add(child);
return child;
}
//定义增加单个子节点的方法
public void addChildren(List> children) {
children.forEach(each -> each.setParent(this));
this.children.addAll(children);
}
//定义增加多个子节点的方法
public Node getParent() {
return parent;
}
//定义获取父节点的方法
public T getData() {
return data;
}
//定义获取特定节点数据的方法
public void setData(T data) {
this.data = data;
}
//定义当前节点数据的设置方法
public void setParent(Node parent) {
this.parent = parent;
}
//定义设置父节点的方法
public void setChildren(List> children) {
this.children = children;
}
//定义设置多个子节点的方法
public List> getChildren() {
return children;
}
//定义获取多个子节点的方法
} //至此,树型结构构造完毕
public void main(String[] args) {
Node root = new Node<>("root");
Node node1 = root.addChild(new Node("中国"));
Node node2 =node1.addChild(new Node("北京市"));
Node node3 =node2.addChild(new Node("朝阳区"));
Node node4 =node2.addChild(new Node("海淀区"));
Node node5 =node1.addChild(new Node("湖北省"));
Node node6 =node5.addChild(new Node("武汉市"));
Node node7 =node6.addChild(new Node("江汉区"));
Node node8 =node6.addChild(new Node("江夏区"));
Node node9 =node1.addChild(new Node("湖南省"));
Node node10 =node9.addChild(new Node("长沙市"));
Node node11 =node9.addChild(new Node("郴州市"));
Node node12 =root.addChild(new Node("美国"));
Node node13 =node12.addChild(new Node("加利福尼亚州"));
Node node14 =node12.addChild(new Node("得克萨斯州"));
}
}
// https://www.javagists.com/java-tree-data-structure
工作内容
java任务
codegym任务
课程1
任务1 整只鸭子还不够
使用 Duck 类作为模式,创建 Cat 和 Dog 类。
你认为 Cat 和 Dog 类中的 toString 方法应返回什么?
在 main 方法中,为每个类创建两个对象并在屏幕上显示它们。
Duck 对象已创建并显示在屏幕上。
package zh.codegym.task.task05.task0525;
public class Solution {
public static void main(String[] args) {
Duck duck1 = new Duck();
Duck duck2 = new Duck();
Dog dog1 = new Dog();
Dog dog2 = new Dog();
Cat cat1 = new Cat();
Cat cat2 = new Cat();
System.out.println(duck1);
System.out.println(duck2);
System.out.println(cat1);
System.out.println(cat2);
System.out.println(dog1);
System.out.println(dog2);
//在此编写你的代码
}
public static class Duck {
public String toString() {
return "Duck";
}
}
public static class Cat {
public String toString() {
return "Cat";
}
}
public static class Dog {
public String toString() {
return "Dog";
}
}
//在此编写你的代码
}
任务2 男人和女人
1.在 Solution 类内部,创建 public static Man 和 public static Woman 类。
2.该类必须包含以下字段:String name, int age, String address.
3.创建包含所有可能参数的构造方法。
4.使用构造方法为每个类创建两个包含完整数据的对象。
5.在屏幕上使用以下格式显示对象:name + " " + age + " " + address
package zh.codegym.task.task05.task0526;
public class Solution {
public static void main(String[] args) {
Man man1 = new Man("xxm", 18, "CSC");
Man man2 = new Man("LGD", 5, "PSG");
Woman woman1 = new Woman("yyq", 35, "CSC");
Woman woman2 = new Woman("Red", 23, "Rhodes");
System.out.println(man1);
System.out.println(man2);
System.out.println(woman1);
System.out.println(woman2);
}
public static class Man {
String name;
int age;
String address;
public Man(String name, int age, String address) {
this.name = name;
this.address = address;
this.age = age;
}
public String toString() {
return (name + " " + age + " " + address);
}
}
public static class Woman {
String name;
int age;
String address;
public Woman(String name, int age, String address) {
this.name = name;
this.address = address;
this.age = age;
}
public String toString() {
return (name + " " + age + " " + address);
}
}
}



