开发一个泛型Apple类,要求有一个重量属性weight在测试类中实例化不同的泛型对象,要求对象a1的这一属性是String类型,对象a2的这一属性是Integer型,a3的这一属性是Double型。分别为a1,a2,a3的重量属性赋值为:”500克”,500,500.0,在测试类中通过对象调用访问器得到属性值并输出。另外思考,为什么a2和a3的属性需要是Integer和Double而不是int和double?
/*开发一个泛型Apple类,要求有一个重量属性weight在测试类中实例化不同的泛型对象*/
public class Apple<T>{
T Weight;
public Apple() {
}
public Apple(T weight) {
Weight = weight;
}
public T getWeight() {
return Weight;
}
public void setWeight(T weight) {
Weight = weight;
}
@Override
public String toString() {
return "Apple{" +
"Weight=" + Weight +
'}';
}
/*要求对象a1的这一属性是String类型,对象a2的这一属性是Integer型,
* a3的这一属性是Double型。分别为a1,a2,a3的重量属性赋值为:”500克”,500,500.0*/
public static void main(String[] args) {
Apple<String> a1 = new Apple<>("500克");
Apple<Integer> a2 = new Apple<>(500);
Apple<Double> a3 = new Apple<>(500.0);
/*在测试类中通过对象调用访问器得到属性值并输出*/
System.out.println("a1:"+a1.getWeight());
System.out.println("a2:"+a2.getWeight());
System.out.println("a3:"+a3.getWeight());
}
}
/*为什么a2和a3的属性需要是Integer和Double而不是int和double?
* 这是因为泛型类型必须是类,不能是基本数据类型,所以需要用到基本数据类型的位置就要用包装类来替换
* */
封装一个新闻类News,包含新闻标题,新闻作者,新闻内容,新闻类型三个属性,提供必要的访问器和修改器方法,重写toString方法,要求打印对象时输出格式为“标题;类型;作者”,要求只要新闻标题相同就判断为同一条新闻。在测试类中创建一个只能容纳该类对象的ArrayList集合,添加三条新闻。遍历集合,打印新闻标题,将新闻标题截取字符串到10个汉字的长度。
/**
* @author Eightn0
* @create 2021-03-18 22:10
* 封装一个新闻类News,包含新闻标题,新闻作者,新闻内容,新闻类型三个属性,
* 提供必要的访问器和修改器方法,重写toString方法,要求打印对象时输出格式为“标题;类型;作者”,
* 要求只要新闻标题相同就判断为同一条新闻。
* 在测试类中创建一个只能容纳该类对象的ArrayList集合,添加三条新闻。
* 遍历集合,打印新闻标题,将新闻标题截取字符串到10个汉字的长度。
*/
/*封装一个新闻类News,包含新闻标题,新闻作者,新闻内容,新闻类型三个属性*/
class Society{
}
class Entertainment{}
public class News<T> {
T Type;
String title;
String author;
String body;
public News() {
}
public News( String title, String author, String body) {
this.title = title;
this.author = author;
this.body = body;
}
/*提供必要的访问器和修改器方法*/
public T getType() {
return Type;
}
public void setType(T type) {
Type = type;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
/*重写toString方法,要求打印对象时输出格式为“标题;类型;作者”*/
@Override
public String toString() {
return "News{" +
", title='" + title +'\'' +
"Type=" + Type + '\'' +
", author='" + author +
'}';
}
/*要求只要新闻标题相同就判断为同一条新闻。*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
News<?> news = (News<?>) o;
return Objects.equals(title, news.title);
}
/*在测试类中创建一个只能容纳该类对象的ArrayList集合,添加三条新闻。*/
public static void main(String[] args) {
ArrayList<News> newsList = new ArrayList<>(3);
newsList.add(new News<Society>("好消息,大降价","Ines","大降价大降价大降价啦"));
newsList.add(new News<Society>("坏消息,大涨价","Ines","大涨价大涨价大涨价啦"));
newsList.add(new News<Entertainment>("是的我们有一个孩子","Tony","孩子在哪里在哪里在哪里"));
/*遍历集合,打印新闻标题,将新闻标题截取字符串到10个汉字的长度。*/
Iterator<News> iterator = newsList.iterator();
while (iterator.hasNext()){
News news = iterator.next();
System.out.println(news.getTitle());
}
}
}
按要求完成下列任务
- 使用HashMap类实例化一个Map类型的对象m1,键(String类型)和值(int型)分别用于存储员工的姓名和工资,存入数据如下: 张三——800元;李四——1500元;王五——3000元;
- 将张三的工资更改为2600元
- 为所有员工工资加薪100元;
- 遍历集合中所有的员工
- 遍历集合中所有的工资
public class HashMapTest {
public static void main(String[] args) {
/*使用HashMap类实例化一个Map类型的对象m1,键(String类型)和值(int型)分别用于存储员工的姓名和工资,*/
HashMap<String,Integer> hashMap = new HashMap<>();
/*存入数据如下: 张三——800元;李四——1500元;王五——3000元*/
hashMap.put("张三",800);
hashMap.put("李四",1500);
hashMap.put("王五",3000);
/*将张三的工资更改为2600元*/
hashMap.replace("张三",2500);
/*为所有员工工资加薪100元*/
/*遍历集合中所有的员工,遍历集合中所有的工资*/
Set<Map.Entry<String,Integer>> entries = hashMap.entrySet();
Iterator<Map.Entry<String,Integer>> iterator = entries.iterator();
while (iterator.hasNext()){
Map.Entry<String, Integer> entry = iterator.next();
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
}
}