用java语言自己开发一个集合类,包含的实例方法如下:向该集合中添加元素时,只能添加某一种类型的
2个回答
展开全部
下面的Test类就是一个集合类,它只能添加Student类型的对象,代码如下:
import java.util.Collection;
import java.util.Iterator;
public class Test implements Collection<Student>{
public static void main(String[] args){
Test t = new Test();
t.add(new Student());
}
@Override
public int size() {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean contains(Object o) {
// TODO Auto-generated method stub
return false;
}
@Override
public Iterator<Student> iterator() {
// TODO Auto-generated method stub
return null;
}
@Override
public Object[] toArray() {
// TODO Auto-generated method stub
return null;
}
@Override
public <T> T[] toArray(T[] a) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean add(Student e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean remove(Object o) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean containsAll(Collection<?> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean addAll(Collection<? extends Student> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean removeAll(Collection<?> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean retainAll(Collection<?> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public void clear() {
// TODO Auto-generated method stub
}
}
class Student{
String stuId;
String name;
public Student(){
}
public String getStuId(){
return stuId;
}
public void setStuId(String stuId){
this.stuId = stuId;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
}
import java.util.Collection;
import java.util.Iterator;
public class Test implements Collection<Student>{
public static void main(String[] args){
Test t = new Test();
t.add(new Student());
}
@Override
public int size() {
// TODO Auto-generated method stub
return 0;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean contains(Object o) {
// TODO Auto-generated method stub
return false;
}
@Override
public Iterator<Student> iterator() {
// TODO Auto-generated method stub
return null;
}
@Override
public Object[] toArray() {
// TODO Auto-generated method stub
return null;
}
@Override
public <T> T[] toArray(T[] a) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean add(Student e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean remove(Object o) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean containsAll(Collection<?> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean addAll(Collection<? extends Student> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean removeAll(Collection<?> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean retainAll(Collection<?> c) {
// TODO Auto-generated method stub
return false;
}
@Override
public void clear() {
// TODO Auto-generated method stub
}
}
class Student{
String stuId;
String name;
public Student(){
}
public String getStuId(){
return stuId;
}
public void setStuId(String stuId){
this.stuId = stuId;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询