码迷,mamicode.com
首页 > 编程语言 > 详细

Java范式1

时间:2018-05-29 17:10:50      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:xtend   hang   util   pac   set   ati   stat   void   array   

package Xwxx;

public class Person {
    private String name;
    private int age;
    public Person() {
	
	}
    public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + "]";
	}   
}
package Xwxx;

public class Student extends Person{
	
	private String name;
	private int age;
    public Student(){
    	
    }
	public Student(String name, int age) {
		this.name=name;
		this.age=age;
				
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + "]";
	}

}

 

package Xwxx;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class Test {

	public static void main(String[] args) {
		ArrayList<Person> list=new ArrayList<Person>();
       list.add(new Person("zhangsan",28));
       list.add(new Person("lisi",18));
       printCollection(list);
       ArrayList<Student> l=new ArrayList<Student>();
       l.add(new Student("小明",22));
       l.add(new Student("小红",20));
       printCollection(l);
	}
    public static void printCollection(Collection<?> list){
    	//变成了object类了,所以只能访问object的方法
    	//? extends Person是接收Person和其子类
    	//? super Student是接收Student和其父类
    	Iterator<?> it=list.iterator();
    	while(it.hasNext()){
    		System.out.println(it.next());
    	}
    }
}

 

 

Java范式1

标签:xtend   hang   util   pac   set   ati   stat   void   array   

原文地址:https://www.cnblogs.com/hankai2735/p/9105782.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!