学习内容
1.占位符T
<T> T 表示的是返回值T是泛型,T是一个占位符传递什么类型的数据,就会返回同类型的数据。
单独的T就是表示限制你传递的参数类型。
2.树形结构的构造
只构造了树形结构,还没有遍历。
可以说的内容基本都卸载代码的注释里了
代码如下:
package com.xxm.exam1;
import java.util.ArrayList;
import java.util.List;
public class task29 {
public class Node<T> {
//定义Note类
private T data = null;
//定义并初始化
private List<Node<T>> children = new ArrayList<>();
//声明并初始化Node类的子节点
private Node<T> parent = null;
//声明并初始化Node类的父节点
public Node(T data) {
this.data = data;
}
//定义Node类数据的设置方法
public Node<T> addChild(Node<T> child) {
child.setParent(this);
this.children.add(child);
return child;
}
//定义增加单个子节点的方法
public void addChildren(List<Node<T>> children) {
children.forEach(each -> each.setParent(this));
this.children.addAll(children);
}
//定义增加多个子节点的方法
public Node<T> getParent() {
return parent;
}
//定义获取父节点的方法
public T getData() {
return data;
}
//定义获取特定节点数据的方法
public void setData(T data) {
this.data = data;
}
//定义当前节点数据的设置方法
public void setParent(Node<T> parent) {
this.parent = parent;
}
//定义设置父节点的方法
public void setChildren(List<Node<T>> children) {
this.children = children;
}
//定义设置多个子节点的方法
public List<Node<T>> getChildren() {
return children;
}
//定义获取多个子节点的方法
} //至此,树型结构构造完毕
public void main(String[] args) {
/*示例
Node<String> root = new Node<>("root");
//定义根节点
Node<String> node1 = root.addChild(new Node<String>("node 1"));
//增加一个结子节点,其父节点是root
*/
Node<String> root = new Node<>("root");
Node<String> node1 = root.addChild(new Node<String>("中国"));
Node<String> node2 =node1.addChild(new Node<String>("北京市"));
Node<String> node3 =node2.addChild(new Node<String>("朝阳区"));
Node<String> node4 =node2.addChild(new Node<String>("海淀区"));
Node<String> node5 =node1.addChild(new Node<String>("湖北省"));
Node<String> node6 =node5.addChild(new Node<String>("武汉市"));
Node<String> node7 =node6.addChild(new Node<String>("江汉区"));
Node<String> node8 =node6.addChild(new Node<String>("江夏区"));
Node<String> node9 =node1.addChild(new Node<String>("湖南省"));
Node<String> node10 =node9.addChild(new Node<String>("长沙市"));
Node<String> node11 =node9.addChild(new Node<String>("郴州市"));
Node<String> node12 =root.addChild(new Node<String>("美国"));
Node<String> node13 =node12.addChild(new Node<String>("加利福尼亚州"));
Node<String> node14 =node12.addChild(new Node<String>("得克萨斯州"));
}
}
// 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);
}
}
}