Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Example – Using Enum constructors and methods

Java Examples - Using Enum Constructors and Methods

Java Examples Java Examples

The following example demonstrates the use of Enum constructors and methods:

Main.java File

enum

Car

{

lamborghini

(

900

)

,

tata

(

2

)

,

audi

(

50

)

,

fiat

(

15

)

,

honda

(

12

)

;

private

int

price

;

Car

(

int

p

)

{

price

=

p

;

}

int

getPrice

(

)

{

return

price

;

}

}

public

class

Main

{

public

static

void

main

(

String

args

[

]

)

{

System

.

out

.

println

(

"

Prices of all cars:

"

)

;

for

(

Car

c

:

Car

.

values

(

)

)

System

.

out

.

println

(

c

"

Needed

"

c

.

getPrice

(

)

"

thousand dollars.

"

)

;

}

}

The output of the above code is:


Prices of all cars:
lamborghini 需要 900 千美元。
tata 需要 2 千美元。
audi 需要 50 千美元。
fiat 需要 15 千美元。
honda 需要 12 千美元。

Java Examples Java Examples