栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA泛型的简单实用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA泛型的简单实用

编程实现:设计一个ArrayList泛型类的对象,统计学生对象的数量.

学生类Student中有属性account和password,还有包含两个参数的构造方法,

已知学生信息为:FXStudent s1=new FXStudent("admin","123456");
                           FXStudent s2=new FXStudent("guest","654321");

请完成统计学生对象数量的程序代码.

public class FXStudent {
	private String account;//姓名
	private String password;//密码
	
	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public FXStudent(String account,String password) {
		this.account=account;
		this.password=password;
	}
}

测试程序 

import java.util.ArrayList;

public class FXStudentTest {

	public static void main(String[] args) {
		//泛型:参数化类型,操作的数据类型被定义为一个参数,并且参数类型可以用在类,接口,方法的创建中
		ArrayList a=new ArrayList();//定义了一个FXStudent类型
		FXStudent s1=new FXStudent("admin","123456");
		FXStudent s2=new FXStudent("guest","654321");
		a.add(s1);//向ArrayList中添加数据
		a.add(s2);
		System.out.println("学生对象的数量:"+a.size());//size()用于获取泛型集合有多少个元素,
		//length()方法:用于获取字符串和数组长度;
	}

}

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/925087.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号