매거 유형enum의 사용
2361 단어 StringobjectnullClasswhitespace
private 구조기가 있고 constant 정의가 처음부터 있어야 합니다
package com.enums;
public enum Size
{
//
SMALL("S"), MEDIUM("M"),LARGE("L");
private String abbrev;
public String getAbbrev(){
return this.abbrev;
}
private Size(String abbrev){
this.abbrev = abbrev;
}
public static void main(String[] args){
for(Size s: Size.values()){
System.out.println(s + "\t" + s.getAbbrev() + "\t" + s.toString());
}
System.out.println(Size.SMALL);
// System.out.println(Size.valueOf("S"));
System.out.println(Size.valueOf("SMALL"));
System.out.println(Size.valueOf(Size.class, "SMALL"));
}
}
원본 코드
/**
* Returns the enum constant of the specified enum type with the
* specified name. The name must match exactly an identifier used
* to declare an enum constant in this type. (Extraneous whitespace
* characters are not permitted.)
*
* @param enumType the <tt>Class</tt> object of the enum type from which
* to return a constant
* @param name the name of the constant to return
* @return the enum constant of the specified enum type with the
* specified name
* @throws IllegalArgumentException if the specified enum type has
* no constant with the specified name, or the specified
* class object does not represent an enum type
* @throws NullPointerException if <tt>enumType</tt> or <tt>name</tt>
* is null
* @since 1.5
*/
public static <T extends Enum<T>> T valueOf(Class<T> enumType,
String name) {
T result = enumType.enumConstantDirectory().get(name);
if (result != null)
return result;
if (name == null)
throw new NullPointerException("Name is null");
throw new IllegalArgumentException(
"No enum const " + enumType +"." + name);
}
여기의valueOf의 첫 번째 매개 변수는 클래스입니다. 클래스마다 클래스의 대상이기 때문에 클래스
반환 유형은 하나의 클래스가 필요하고 매개 변수는 하나의 클래스의Class가 필요합니다.
들어오는name은 클래스에 정의되어 있어야 합니다. 예를 들어 SMALL MEDIUM LARGE
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Access Request, Session and Application in Struts2If we want to use request, Session and application in JSP, what should we do? We can obtain Map type objects such as Req...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.