例题1
package 例题;
interface SeasonInterface{
//四季接口
int SPRING = 1, SUMMER = 2, AUTUMN = 3, WINTER = 4;
}
enum SeasonEnum{
//四季枚举
SPRING,SUMMER,AUTUMN,WINTER
}
public class 例题1 {
//定义方法
public static void printSeason1(int season){
switch (season) {
case SeasonInterface.SPRING:
System.out.println("这是春季");
break;
case SeasonInterface.SUMMER :
System.out.println("这是夏季");
break;
case SeasonInterface.AUTUMN :
System.out.println("这是秋季");
break;
case SeasonInterface.WINTER :
System.out.println("这是冬季");
break;
default:
System.out.println("这不是四季常量值");
break;
}
}
public static void printSeason2(SeasonEnum season){
switch (season) {
case SPRING:
System.out.println("这是春季");
break;
case SUMMER :
System.out.println("这是夏季");
break;
case AUTUMN :
System.out.println("这是秋季");
break;
case WINTER :
System.out.println("这是冬季");
break;
}
}
public static void main(String[] args) {
//使用接口常量做参数
printSeason1(SeasonInterface.SPRING);
//使用数字做参数
printSeason1(3);
//使用接口常量值以外的数字冒充常量值
printSeason1(-1);
//使用枚举做参数
printSeason2(SeasonEnum.WINTER);
}
}
结果
例题2
package 例题;
//定义枚举
enum Season{
SPRING, SUMMER, AUTUMN, WINTER
}
public class 例题2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//使用枚举方法values
Season se[] = Season.values();
for(int i = 0; i < se.length; i++) {
System.out.println("枚举常量:" + se[i]);
}
}
}
结果
例题3
package 例题;
enum Season8 {
SPRING, SUMMER, AUTUMN, WINTER
}
public class 例题3 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Season8 se = Season8.valueOf("SUMMER");//根据字符串创建一个枚举值
Season8 se1[] = Season8.values();//获取所有枚举值
for(int i = 0; i<se1.length;i++ ) {
String message = "";//待输出消息
int result = se.compareTo(se1[i]);//记录两个枚举比较的结果
if(result < 0) {
message = se + "在" + se1[i] + "的前" + (-result) + "个位置";
}
else if(result>0) {
message = se + "在" + se1[i] + "的后" + (result) + "个位置";
}
else if(result==0) {
message = se + "与" + se1[i] + "是同一个值" ;
}
System.out.println(message);
}
}
}
结果
例题4
package 例题;
enum SE{
SPRING, SUMMER, AUTUMN, WINTER
}
public class 例题4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
SE se[] = SE.values();
for(int i = 0; i < se.length; i++) {
System.out.println(se[i] + "在枚举类型中位置索引值" + se[i].ordinal());
}
}
}
结果
例题5
package 例题;
enum Seaon{
SPRING("万物复苏"),
SUMMER("烈日炎炎"),
AUTUMN("秋草枯黄"),
WINTER("白雪皑皑");
public String remarks;//枚举的备注
private Seaon(String remarks) {//构造方法
this.remarks = "我是" + this.toString() + "我来之后" + remarks + "。";
}
public String getRemarks() {//获取备注方法
return remarks;
}
}
public class 例题5 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Seaon ss[] = Seaon.values();
for(int i = 0; i < ss.length; i++) {
System.out.println(ss[i].getRemarks());
}
}
}
结果
例题6
package 例题;
public class 例题6<T> {
private T bookinfo;
public 例题6(T bookinfo) {
this.bookinfo = bookinfo;
}
public 例题6() {
// TODO Auto-generated constructor stub
}
public T getBookinfo() {
return bookinfo;
}
public static void main(String[] args) {
//创建参数String类型的书名对象
例题6<String> bookName = new 例题6("Java从入门到精通");
// 创建参数String类型的作者对象
例题6<String> bookAuthor = new 例题6("明日科技");
//创建参数String类型的价格对象
例题6<Double> bookPrice = new 例题6(69.8);
//创建参数String类型的附赠源码
例题6<Boolean> hasSource = new 例题6(true);
System.out.println("书名:" + bookName.getBookinfo());
System.out.println("作者:" + bookAuthor.getBookinfo());
System.out.println("价格:" + bookPrice.getBookinfo());
System.out.println("是否附赠源码:" + hasSource.getBookinfo());
}
}
结果
例题7
package 例题;
public class 例题7<A> {
private A[] array;
public A[] getarray() {
return array;
}
public void setArry(A[] array) {
this.array = array;
}
public static void main(String[] args) {
例题7<String> dome = new 例题7();
String value[] = {"成员1", "成员2", "成员3", "成员4", "成员5"};
dome.setArry(value);
String array[] = dome.getarray();
for(int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
}
结果
例题8
package 例题;
import java.util.*;
public class 例题8 {
public static void main(String[] args) {
//定义ArrayList容器,设置容器内是的值为Integer
ArrayList<Integer> a = new ArrayList<Integer>();
a.add(1);
for(int i = 0; i<a.size(); i++) {
//根据容器长度,循环显示容器内的值
System.out.println("获取ArrayList容器成员的值:" + a.get(i));
}
//定义HashMap容器,设置容器的键名与键值类型分别为Integer型与String型
HashMap<Integer ,String> m = new HashMap<Integer ,String>();
for(int i = 0; i<5; i++) {
m.put(i, "成员" + i);
}
for(int i = 0; i<m.size(); i++) {
System.out.println("获取Map容器成员的值;" + m.get(i));
}
}
}
结果文章来源:https://www.toymoban.com/news/detail-739846.html
文章来源地址https://www.toymoban.com/news/detail-739846.html
到了这里,关于第十三章,枚举与泛型例题的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!