1 内部类
1.1 格式及概念
概念:将一类A定义在类B里面,A是内部类,则B称为外部类。
class B{
//成员内部类
class A{
// 类A的内容
}
}
1.2 访问特点
- 内部类可以直接访问外部成员,包括私有成员
- 外部类要访问内部类成员,必须要建立内部类的对象
- 格式外部类名.内部类名 对象名 = new外部类型().new 内部类型();
//先创建外部对象,再创建内部对象
Person person = new Person();
Person.Heart heart = person.new Heart();
//直接创建内部对象
Person.Heart heart = new Person().new Heart();
1.3案例
public class Person {
private boolean live = true;
//创建内部类
class Heart{
public void jump(){
//直接访问外部成员
if(live){
System.out.println("有心跳");
}else{
System.out.println("Game over");
}
}
}
public boolean isLive(){
return live;
}
public void setLive(boolean live){
this.live = live;
}
}
public class InnerDemoTest {
public static void main(String[] args) {
// 创建外部类对象
Person person = new Person();
// 创建内部类对象
// 格式外部类名.内部类名 对象名 = new外部类型().new 内部类型();
Person.Heart heart = person.new Heart();
//或者直接创建内部类对象
//Person.Heart heart1 = new Person().new Heart();
// 调用内部类方法
heart.heartBeat();
// 调用外部类方法
boolean live = person.isLive();
System.out.println(live);
person.setLive(false);
heart.heartBeat();
}
}
1.4 匿名内部类
概念:是内部类的简化写法,本质是一个带具体实现的父类或者父接口的匿名的子类对象
简化步骤:当你使用一个接口时,需要:1.定义子类 2.重写接口中的方法 3.创建子类对象 4.调用重写后的方法。如果我们的目的是调用重写后的方法,那么使用匿名内部类可以将四步简化成一步。
前提:匿名内部类必须继承一个父类或者实现一个父接口。
new 父类名或接口名(){
//方法重写
@Override
public void method(){
//执行语句
}
};
- 匿名内部类不能定义任何静态成员、方法。
- 匿名内部类中的方法不能是抽象的;
- 匿名内部类必须实现接口或抽象父类的所有抽象方法。
- 匿名内部类访问的外部类成员变量或成员方法必须用static修饰;
public abstract class FlyAble {
public abstract void fly();
}
public class InnerDemoTest2 {
public static void main(String[] args) {
// 抽象方法本来不能被创建对象,但是可以作为内部类将抽象方法重写调用
FlyAble f = new FlyAble() {
@Override
public void fly() {
System.out.println("起飞");
}
};
f.fly();
}
}
匿名内部类作为参数进行传递
ublic class InnerDemoTest3 {
public static void main(String[] args) {
/**
* 1. 等号右边 ,定义并创建该接口的子类对象
* 2. 等号左边。 是多态,接口类型引用指向子类对象
*/
FlyAble f = new FlyAble(){
@Override
public void fly() {
System.out.println("我要起飞了");
}
};
// 将带有匿名内部类作为方法的参数进行了传递
showFly(f);
}
public static void showFly(FlyAble f){
f.fly();
}
}
简化
public class InnerDemoTest4 {
public static void main(String[] args) {
// 实际开发中常用的形式
showFly(new FlyAble(){
@Override
public void fly() {
System.out.println("我要起飞了");
}
});
}
public static void showFly(FlyAble f){
f.fly();
}
}
2 引用类型用法
2.1类作为成员变量和方法的参数
public class Weapon {
private String name; //武器名
private int hurt; //武器伤害
public Weapon(String name, int hurt) {
this.name = name;
this.hurt = hurt;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHurt() {
return hurt;
}
public void setHurt(int hurt) {
this.hurt = hurt;
}
}
public class Armour {
private String name; //防具名
private int protect; //防具值
public Armour(String name, int protect) {
this.name = name;
this.protect = protect;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getProtect() {
return protect;
}
public void setProtect(int protect) {
this.protect = protect;
}
}
public class Role {
private int id; //角色id
private int blood; // 角色血量
private String name; // 角色名
//添加武器属性
private Weapon wp;
//添加盔甲属性
private Armour ar;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getBlood() {
return blood;
}
public void setBlood(int blood) {
this.blood = blood;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Weapon getWp() {
return wp;
}
public void setWp(Weapon wp) {
this.wp = wp;
}
public Armour getAr() {
return ar;
}
public void setAr(Armour ar) {
this.ar = ar;
}
public void Attack(){
System.out.println("使用" + wp.getName() + ",造成了" + wp.getHurt() + "点伤害");
}
public void Wear(){
System.out.println("穿上了" + ar.getName() + ",增加了" + ar.getProtect() + "点生命值");
}
}
public class GameTest {
public static void main(String[] args) {
Weapon weapon = new Weapon("M4A1",1314);
Armour armour = new Armour("三级甲",666);
Role role = new Role();
role.setWp(weapon);
role.setAr(armour);
role.Attack(); //使用M4A1,造成了1314点伤害
role.Wear(); //穿上了三级甲,增加了666点生命值
}
}
1.成员变量
public method{
private String name
}
private weapon wp
2.方法的参数
role.setWp(weapon);
role.setAr(armour);
3.作为方法的返回值
public Armour getAr() {
return ar;
}
2.2接口作为成员变量和方法的参数
public interface FaShuSkill {
public abstract void faShuAttack();
}
public class WangZheRole {
private FaShuSkill faShuSkill;
public FaShuSkill getFaShuSkill() {
return faShuSkill;
}
public void setFaShuSkill(FaShuSkill faShuSkill) {
this.faShuSkill = faShuSkill;
}
public void faShuSkillAttack(){
System.out.println("开始攻击");
faShuSkill.faShuAttack();
System.out.println("释放结束");
}
}
public class InnerDemoTest5 {
public static void main(String[] args) {
WangZheRole role = new WangZheRole();
// 设置角色法术技能
role.setFaShuSkill(new FaShuSkill() {
@Override
public void faShuAttack() {
System.out.println("发射爱心");
}
});
role.faShuSkillAttack();
role.setFaShuSkill(new FaShuSkill() {
@Override
public void faShuAttack() {
System.out.println("发射激光");
}
});
role.faShuSkillAttack();
}
}
1.作为成员变量
private FaShuSkill faShuSkill;
2.作为方法的参数
public void setFaShuSkill(FaShuSkill faShuSkill) {
this.faShuSkill = faShuSkill;
}
role.setFaShuSkill(new FaShuSkill() {
@Override
public void faShuAttack() {
System.out.println("发射爱心");
}
});
3.作为方法的返回值
public FaShuSkill getFaShuSkill() {
return faShuSkill;
}
3 Java高级API
3.1Object类
概念:类 Object 是类层次结构的根类。每个类都使用 Object 作为超类。所有对象(包括数组)都实现这个类的方法。
如果一个类没有指定父类,那他默认继承Object类。
包含十一个方法
3.1.1 toString方法
概念:返回对象的字符串表示或返回对象的全限定名+@+内存地址
如果希望输出对象的基本信息,需要重写toString方法
public class Person {
private String name;
private int age;
private boolean live = true;
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", live=" + live +
'}';
}
}
public class Main{
public static void main(String[] args) {
Person person1 = new Person("皮皮虾", 14);
Person person2 = new Person("皮皮虾", 14);
System.out.println(person1.equals(person2));
}
}
3.1.2 equals方法
- 指示其他某个对象是否与此对象“相等”
- 默认是地址值的比较 == ,只要不少同一个对象必然是false
@Override
// 比较原则, name 和 age 相同就是同一个person
public boolean equals(Object o) {
// 如果对象地址一样,则认为相同
if (this == o)
return true;
// 如果参数为空, 或者类型不一样,则认为不同
if (o == null || getClass() != o.getClass())
return false;
// 转换为当前类型
Person person = (Person) o;
// 应用类型比较相等使用 java.util.Objects;的equals静态方法 取得结果
return age == person.age && Objects.equals(name, person.name);
}
3.1.3 getClass方法
返回Object的运行时类
3.2 Objects类
equals:Object的equals方法容易抛出空指针异常,Objects进行了优化
public static boolean equals(Object a, Object b) {
return (a == b) || (a != null && a.equals(b));
}
练习题:
案例一:
public class Student {
String name;
boolean attendance = true;
public Student(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isAttendance() {
return attendance;
}
public void setAttendance(boolean attendance) {
this.attendance = attendance;
}
}
public class Teacher {
String name;
Student student;
public Teacher(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void dianMing(Student student) {
this.student = student;
student.setAttendance(false);
}
}
public class Course {
private String name;
private Teacher teacher;
private Student student;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
//ArrayList<Student> students
public void show(List<Student> students,Teacher teacher){
System.out.println("课程信息:" + this.name + ",老师姓名:" + teacher.getName());
for (int i = 0; i < students.size(); i++) {
if(students.get(i).isAttendance()){
System.out.println("上课:" + students.get(i).getName());
}else{
System.out.println("旷课:" + students.get(i).getName());
}
}
}
}
public class CourseTest {
public static void main(String[] args) {
Student student1 = new Student("小红");
Student student2 = new Student("小亮");
Student student3 = new Student("小明");
Teacher teacher = new Teacher("张老师");
teacher.dianMing(student3);
ArrayList<Student> students = new ArrayList<Student>();
students.add(student1);
students.add(student2);
students.add(student3);
Course course = new Course();
course.setName("Java");
course.show(students,teacher);
}
}
案例二:
public interface FightAble {
void specialFight();
default void commonFight(){
System.out.println("普通打击");
}
}
public class Warrior implements FightAble {
@Override
public void specialFight() {
System.out.println("武器攻击");
}
}
public class Master implements FightAble{
@Override
public void specialFight() {
System.out.println("法术攻击");
}
}
public class Player {
public FightAble select(String str){
if("法力角色".equals(str)){
return new Master();
}else if ("武力角色".equals(str)){
return new Warrior();
}
return null;
}
}
public class Test {
public static void main(String[] args) {
Player player = new Player();
String select = "法力角色";
player.select(select);
System.out.println("选择:" + select);
FightAble master = new Master();
master.commonFight();
master.specialFight();
System.out.println("=========");
String select1 = "武力角色";
player.select(select);
System.out.println("选择:" + select);
FightAble warrior = new Warrior();
warrior.commonFight();
warrior.specialFight();
}
}
案例四:
public class Apple {
private int size;
private String color;
public Apple() {
}
public Apple(int size, String color) {
this.size = size;
this.color = color;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return this.size + "-" + this.color;
}
}
public interface CompareAble {
default Apple compare(Apple a1,Apple a2){
return a1.getSize() > a2.getSize()? a1:a2;
}
}
public class Compare implements CompareAble {
}
public class Worker {
public Apple pickApple(CompareAble compareAble,Apple a1,Apple a2){
Apple apple = compareAble.compare(a1, a2);
return apple;
}
}
public class Test {
public static void main(String[] args) {
Worker worker = new Worker();
System.out.println("默认挑大的:");
Apple apple1 = new Apple(5,"青色");
Apple apple2 = new Apple(3,"红色");
Apple apple = worker.pickApple(new Compare(),apple1, apple2);
System.out.println(apple);
Apple appleHong = worker.pickApple(new Compare() {
@Override
public Apple compare(Apple a1, Apple a2) {
System.out.println("挑红的:");
return "红色".equals(a1.getColor()) ? a1 : a2;
}
}, apple1, apple2);
System.out.println(appleHong);
}
}