day3-作业(18-23)

1-instanceof 用法总结.

用法: boolean result = object instanceof class 
参数: 
	Result:布尔类型。 
	Object:必选项。任意对象表达式。 
	Class:必选项。任意已定义的对象类。 
返回值: 如果 object 是 class 的一个实例,则 instanceof 运算符返回 true。如果 object 不是指定类的一个实例,或者 object 是 null,则返回 false

2-泛型:常见的字母及分别对应含义?

T:type 类型
K:key 键
V:value 值
E:element 元素

3-泛型的优点是安全和省心,请用代码说明。

package test;

public class Student<T1, T2> {
	private T1 javaScore;
	private T2 oracleScore;

	public T1 getJavaScore() {
		return javaScore;
	}

	public void setJavaScore(T1 javaScore) {
		this.javaScore = javaScore;
	}

	public T2 getOracleScore() {
		return oracleScore;
	}

	public void setOracleScore(T2 oracleScore) {
		this.oracleScore = oracleScore;
	}
	
	public static void main(String[] args) {
		// 使用时指定类型(引用类型)
		Student<String, Integer> stu = new Student<String, Integer>();
		// 1 安全:类型检查
		stu.setJavaScore("优秀");
		// 2.省心,自动类型转换
		int it = stu.getOracleScore(); // 自动拆箱
	}
}

4-泛型接口注意事项是是么?

接口中泛型字母只能使用在方法上,不能使用在全局常量中

5-泛型方法注意事项是什么?

泛型要放在返回值的前面,只能访问对象的信息,不能修改信息

6-泛型:(1)子类指定具体类型,(2)子类为泛型类,(3)子类为泛型类/父类不指定类型,(4)子类与父类同时不指定类型,以上4点请分别用代码举例。

package test;
/**
 * 泛型接口,与继承同理
 * 重写方法随父类而定
 * @author WL20180732
 *
 * @param <T>
 */
public interface Comparable<T> {
	void compare(T t);
}
// 子类指定具体类型
class Comp implements Comparable<Integer>{

	@Override
	public void compare(Integer t) {
		
	}
}
// 子类与父类同时不指定类型
class Comp1 implements Comparable{

	@Override
	public void compare(Object t) {
		
	} 
}
// 子类为泛型类/父类不指定类型
class Comp2<T> implements Comparable{

	@Override
	public void compare(Object t) {
		
	}
}
//子类为泛型类
class Comp3<T> implements Comparable<T>{
	@Override
	public void compare(T t) {
	}
}

7-泛型接口,方法是以父类而定还是以子类而定?

随父类而定

8-形参使用多态、返回类型使用多态 请分别代码举例。

package test;

public class FruitApp {

	public static void main(String[] args) {
		Fruit f =new Apple();
		test(new Apple());
	}
	//形参使用多态
	public static void test(Fruit f){
		
	}
	//返回类型使用多态
	public static Fruit test2(){
		return new Apple();
	}
}

9-泛型有没有多态?

没有

10-泛型的?问号: 只能声明在类型|方法上,不能声明类或者使用时,请用代码证明这句话的正确性.

package test;

public class Student<T> {
	T score;
	public static void main(String[] args) {
		Student<?> stu = new Student<String>();
		test(new Student<Integer>());
	}
	
	public static void test(Student<?> stu){
		
	}
}

11-泛型嵌套:由外到内拆分.请用代码解释这句话.

package test;

public class Bjsxt <T>{
    T stu ;
     public static void main(String[] args) {
		 //泛型的嵌套
		 Bjsxt<Student<String>> room =new  Bjsxt<Student<String>>();
		 //从外到内拆分
		 room.stu = new Student<String>();
		 Student<String> stu = room.stu;
		 String score =stu.score;
		 System.out.println(score);
	}
}

12-泛型有没有数组?

没有

13-用匿名内部类实现迭代器。

package test;

import java.util.Iterator;

/**
 * 使用匿名内部类 hasNext next
 * 
 * @author Administrator
 *
 */
public class MyArrayList3 implements java.lang.Iterable<String> {
	private String[] elem = { "a", "b", "c", "d", "e", "f", "g" };
	private int size = elem.length;

	public Iterator<String> iterator() {
		return new Iterator<String>() {
			private int cursor = -1;

			public boolean hasNext() {
				return cursor + 1 < size;
			}

			public String next() {
				cursor++;
				return elem[cursor];
			}

			public void remove() {
			}

		};
	}

	public static void main(String[] args) {
		MyArrayList3 list = new MyArrayList3();
		Iterator<String> it = list.iterator();
		while (it.hasNext()) {
			System.out.println(it.next());
			it.remove();  //迭代器可以在遍历时删除元素,foreach不可以
		}

		it = list.iterator();
		while (it.hasNext()) {
			System.out.println(it.next());
		}
		System.out.println("增强for,必须实现java.lang.Iterable接口,重写iterator方法");
		for (String temp : list) {
			System.out.println(temp);

		}
	}
}

14-用分拣思路统计字符串出现次数"this-is-my-first-dog-but-i-like-cat-and-cat-is-nice-and-dog-is-friendly-this-why-i-like-cat-more".

package test;

public class TestMap {

	public static void main(String[] args) {
		String str = "this-is-my-first-dog-but-i-like-cat-and-cat-is-nice-and-dog-is-friendly-this-why-i-like-cat-more";
		String[] strArray = str.split("-");
		
		Map<String, Integer> words = new HashMap<String, Integer>();
		
		for (String temp : strArray) {
			if (words.containsKey(temp)) {
				words.put(temp, words.get(temp) + 1);
			} else {
				words.put(temp, 1);
			}
		}
		Set<String> keys = words.keySet();
		for (String key : keys) {
			System.out.println("单词:" + key + ",次数" + words.get(key));
		}

	}
}

15-用面向对象思想+分拣思路统计班级总人数和平均分。

package test;
/**
 * 学生类
 * @author WL20180732
 *
 */
public class Student {
	// 姓名
	private String name;
	// 学号
	private String no;
	//分数
	private double score;
	
	public Student() {
	}
	public Student(String name, String no, double score) {
		super();
		this.name = name;
		this.no = no;
		this.score = score;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getNo() {
		return no;
	}

	public void setNo(String no) {
		this.no = no;
	}

	public double getScore() {
		return score;
	}

	public void setScore(double score) {
		this.score = score;
	}

	@Override
	public String toString() {
		return "Student [name=" + name + ", no=" + no + ", score=" + score
				+ "]";
	}
}

package test;

import java.util.ArrayList;
import java.util.List;

/**
 * 班级类
 * @author WL20180732
 *
 */
public class ClassRoom {
	// 班号
	private String no;
	// 学生集合
	private List<Student> stus;
	// 总分
	private double total;
	
	public ClassRoom() {
		stus = new ArrayList<Student>();	
	}
	
	public ClassRoom(String no) {
		this();
		this.no = no;	
	}
	public String getNo() {
		return no;
	}
	public void setNo(String no) {
		this.no = no;
	}
	public List<Student> getStus() {
		return stus;
	}
	public void setStus(List<Student> stus) {
		this.stus = stus;
	}
	public double getTotal() {
		return total;
	}
	public void setTotal(double total) {
		this.total = total;
	}
}

package test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
 * 定义一个student类,现将若干student对象放入list,请统计每个班的  总分和平均分,分别打印出来
 * @author WL20180732
 *
 */
public class MapDemo03 {
	public static void main(String[] args) {
		List<Student> list = new ArrayList<Student>();
		exam(list);		
		
		Map<String,ClassRoom> rooms = new HashMap<String,ClassRoom>();
		count(rooms,list);
		printScore(rooms);
	}
	
	public static void printScore(Map<String,ClassRoom> rooms){
		Set<Map.Entry<String,ClassRoom>> entrySet =rooms.entrySet();
		Iterator<Map.Entry<String,ClassRoom>> it =entrySet.iterator();
		while(it.hasNext()){
			Map.Entry<String,ClassRoom> entry =it.next();
			ClassRoom room = entry.getValue();
			double avg = room.getTotal()/room.getStus().size();
			System.out.println("班级:"+room.getNo()+",总分"+room.getTotal()+",平均分"+avg);
		}		
	}
	
	public static void count(Map<String,ClassRoom> rooms,List<Student> list){
		for(Student stu:list){
			String no = stu.getNo();
			double score = stu.getScore();
			// 根据班级编号, 查看Map是否存在该班级 分拣思路
			ClassRoom room = rooms.get(no);
			if(null==room){
				room = new ClassRoom(no);
				rooms.put(no, room);
			}			
			room.setTotal(room.getTotal()+score);
			room.getStus().add(stu);
		}
	}

	public static void exam(List<Student> list){
		list.add(new Student("a","001",80));
		list.add(new Student("b","001",80));
		list.add(new Student("a","002",80));
		list.add(new Student("c","003",80));
		list.add(new Student("d","003",80));
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值