目录
一、枚举
(1)自定义枚举:
public class Hello {
public static void main(String[] args) {
System.out.println(Season.SPRING);
}
}
class Season {//类
private String name;
private String desc; //描述
//定义了四个对象
public final static Season SPRING = new Season("春天","温暖");
public final static Season SUMMER = new Season("夏天","炎热");
public final static Season WINTER = new Season("冬天","寒冷");
public final static Season AUTUMN = new Season("秋天","舒服");
//1构造器私有化
//2去掉setXxx方法,防止属性被修改
//3在Season 内部,直接创建固定的对象
//4优化加入final
private Season(String name, String desc) {
this.name = name;
this.desc = desc;
}
public String getName() {
return name;
}
public String getDesc() {
return desc;
}
@Override
public String toString() {
return "Season{" +
"name='" + name + '\'' +
", desc='" + desc + '\'' +
'}';
}
}
(2)使用enum关键字:
public class Hello {
public static void main(String[] args) {
System.out.println(Season.SPRING);
}
}
enum Season {//类
//使用enum关键字
//1.使用关键字 enum 替代 class
//2.public final static Season SPRING = new Season("春天","温暖");
// 直接用 Spring("春天","温暖")代替 常量名(实参列表)
//3.多个常量使用,号间隔
//4.使用enum 来实现枚举,要求将常量定义对象,写在前面
SPRING("春天","温暖"),
SUMMER("夏天","炎热"),
WINTER("冬天","寒冷"),
AUTUMN("秋天","舒服");
private String name;
private String desc; //描述
private Season(String name, String desc) {
this.name = name;
this.desc = desc;
}
public String getName() {
return name;
}
public String getDesc() {
return desc;
}
@Override
public String toString() {
return "Season{" +
"name='" + name + '\'' +
", desc='" + desc + '\'' +
'}';
}
}
细节单独写一个 WINTER调用的是无参构造
(3)enum常用的方法:
public class Hello {
public static void main(String[] args) {
Season autumn = Season.AUTUMN;
System.out.println(autumn.name()); //输出这个枚举对象的名称
System.out.println(autumn.ordinal()); //输出的是该枚举的次序/编号,从零开始
Season[] values = Season.values(); //返回一个数组
for (Season season:values){ //增强for循环
System.out.println(season);
}
}
}
enum Season {//类
SPRING("春天","温暖"),
SUMMER("夏天","炎热"),
WINTER("冬天","寒冷"),
AUTUMN("秋天","舒服");
private String name;
private String desc; //描述
private Season(String name, String desc) {
this.name = name;
this.desc = desc;
}
public String getName() {
return name;
}
public String getDesc() {
return desc;
}
@Override
public String toString() {
return "Season{" +
"name='" + name + '\'' +
", desc='" + desc + '\'' +
'}';
}
}
(4)案例:声明Week类,其中包含星期一到星期日的定义:MONDAY,TUESDAY,WEDNSDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY,遍历
public class Hello {
public static void main(String[] args) {
Week[] values = Week.values();
System.out.println("====所有星期如下====");
for (Week week : values){
System.out.println(week.getName());
}
}
}
enum Week{
MONDAY("星期一"),
TUESDAY("星期二"),
WEDNESDAY("星期三"),
THURSDAY("星期四"),
FRIDAY("星期五"),
SATURDAY("星期六"),
SUNDAY("星期日");
private String name;
private Week(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Week{" +
"name='" + name + '\'' +
'}';
}
}
二、注解
(1)@Override注解:
只能修饰方法,不能修饰包,类,表示重写
(2)@Deprecated:
修饰一个元素表示一个元素已经过时了,但是任然可以使用
(3)@SuppressWarning({" "}):
all屏蔽所有错误
(4)@Retention注解有三个值:
RetentionPolicy.SOURCE:编译器使用后,直接丢弃这种策略的注释
RetentionPolicy.CLASS:编译会把注解记录在class文件中,当运行java序时,JVM不会保留注解。这是默认值
RetentionPolicy.RUNTIME:编译器把注解记录在class文件中,通过反射获取该注解
三、家庭作业
(1)![](https://i-blog.csdnimg.cn/blog_migrate/22969f466ff0769ea9551f96eb65cb2c.png)
public class Hello {
public static void main(String[] args) {
System.out.println(new Frock().getSerialNumber());
System.out.println(new Frock().getSerialNumber());
System.out.println(new Frock().getSerialNumber());
}
}
class Frock {
private static int currentNum = 100000;
private int serialNumber;
public Frock() {
this.serialNumber = getNextNum();
}
public static int getNextNum() {
currentNum += 100;
return currentNum;
}
public int getSerialNumber() {
return serialNumber;
}
}
(2)
public class Hello {
public static void main(String[] args) {
Animal cat = new Cat();
Animal dog = new Dog();
cat.shout();
dog.shout();
}
}
abstract class Animal {
public abstract void shout();
}
class Cat extends Animal{
@Override
public void shout() {
System.out.println("猫会喵喵叫");
}
}
class Dog extends Animal{
@Override
public void shout() {
System.out.println("狗会汪汪叫");
}
}
(3)
public class Hello {
public static void main(String[] args) {
new Cellphone().testWork(new ICalculate() {
@Override
public double work(double a, double b) {
return a + b;
}
}, 10, 20);
}
}
interface ICalculate {
public double work(double a, double b);
}
class Cellphone {
public void testWork(ICalculate iCalculate, double a, double b) {
double work = iCalculate.work(a, b);
System.out.println("计算结果为:" + work);
}
}
(4)
public class Hello {
public static void main(String[] args) {
new A().f1();
}
}
class A {
private String name = "好好学Java";
public void f1() {
class B {
private String name = "韩顺平教育";
public void show() {
System.out.println(name);
System.out.println(A.this.name);
}
}
new B().show();
}
}
(5)
public class Hello {
public static void main(String[] args) {
new Person("唐僧",new Boat()).common();
new Person("唐僧",new Horse()).passRiver();
}
}
interface Vehicles {
void work();
}
class Horse implements Vehicles {
@Override
public void work() {
System.out.println("使用马走路");
}
}
class Boat implements Vehicles {
@Override
public void work() {
System.out.println("使用船过河");
}
}
class Tools {
public static Horse getHorse() {
return new Horse();
}
public static Boat getBoat() {
return new Boat();
}
}
class Person{
private String name;
private Vehicles vehicles;
public Person(String name, Vehicles vehicles) {
this.name = name;
this.vehicles = vehicles;
}
public void passRiver(){
if (vehicles == null){
vehicles = Tools.getBoat();
}
vehicles.work();
}
public void common(){
if (vehicles == null){
vehicles = Tools.getHorse();
}
vehicles.work();
}
}
(6)
public class Hello {
public static void main(String[] args) {
new Car(45).new Air().flow();
new Car(15).new Air().flow();
new Car(-5).new Air().flow();
}
}
class Car{
private int temperature;
public Car(int temperature) {
this.temperature = temperature;
}
class Air{
public void flow(){
if (Car.this.temperature > 40){
System.out.println("冷风");
return;
}else if(Car.this.temperature < 0){
System.out.println("热风");
return;
}
System.out.println("不开");
}
}
}
(7)![](https://i-blog.csdnimg.cn/blog_migrate/0d622f1de0ad6035999e0c32149b77eb.png)
public class Hello {
public static void main(String[] args) {
Color blue = Color.BLUE;
blue.show();
System.out.println(blue);
switch (blue) {
case RED:
System.out.println("匹配到红色");
break;
case BLUE:
System.out.println("匹配到蓝色");
break;
case YELLOW:
System.out.println("匹配到黄色");
break;
}
}
}
interface ShowColor {
void show();
}
enum Color implements ShowColor {
RED(255, 0, 0),
BLUE(0, 0, 255),
BLACK(0, 0, 0),
YELLOW(255, 255, 0),
GREEN(0, 255, 0);
private int redValue;
private int greenValue;
private int blueValue;
private Color(int redValue, int greenValue, int blueValue) {
this.redValue = redValue;
this.greenValue = greenValue;
this.blueValue = blueValue;
}
public int getRedValue() {
return redValue;
}
public int getGreenValue() {
return greenValue;
}
public int getBlueValue() {
return blueValue;
}
@Override
public void show() {
System.out.println("蓝:" + getBlueValue() + "红:" + getRedValue() + "绿:" + getGreenValue());
}
}