day14 review
Java-day15 study notes
day15
One, permission modifier
Permission modifiers are used to control the scope of use of classes, methods, variables, etc.
public
公开的,最大的权限。
被修饰的内容可以在任何地方被访问(本类、同包下的其它类、不同包下的子类、不同包下的类)
protected
受保护的
被修饰的内容可以在(本类、同包下的其它类、不同包下的子类)中被访问
默认的(包权限)
留空的就是默认的的权限
被修饰的内容可以在(本类、同包下的其它类)中被访问
private
私有的,最小的权限
被修饰的内容只能在(本类)中被访问
Two, anonymous inner class
2.1 Format
new 类名或接口名() {
方法的重写;
}
2.2 Essence
An anonymous inner class is essentially an object (an object of a class or an object of an interface).
Because the interface itself cannot create objects, and anonymous inner classes can directly create objects of the interface. Although ordinary classes can be created directly, anonymous inner classes can also be used to create objects (the same applies to abstract classes)
/*
抽象类
*/
abstract class Animal {
public abstract void eat();
}
/*
解决方案一:
1. 先建立一个子类继承抽象的Animal类,同时重写父类的抽象方法
2. 创建子类的对象
3. 使用子类的对象调用eat方法
*/
//1. 先建立一个子类继承抽象的Animal类,同时重写父类的抽象方法
class CoffeeCat extends Animal {
@Override
public void eat() {
System.out.println("不吃饭,只喝咖啡!");
}
}
public class Demo {
public static void main(String[] args) {
//2. 创建子类的对象
// Animal c = new CoffeeCat(); //多态的向上转型
//3. 使用子类的对象调用eat方法
// c.eat();
/*
解决方案二:
1. 使用匿名内部类得到抽象类Animal的对象
2. 调用eat方法
*/
// new Animal() {
//
// @Override
// public void eat() {
// System.out.println("只干饭,不喝咖啡!");
// }
// }.eat();
Animal a = new Animal() {
@Override
public void eat() {
System.out.println("只干饭,不喝咖啡!");
}
};
a.eat();
}
}
/*
接口A
*/
interface A {
public abstract void show();
}
/*
解决方案一:
1. 先建立一个实现类实现接口A,同时重写接口A中的抽象方法
2. 创建实现类的对象
3. 通过实现类的对象调用你show方法
*/
//1. 先建立一个实现类实现接口A,同时重写接口A中的抽象方法
class AImpl implements A {
@Override
public void show() {
System.out.println("实现类的方式调用show方法");
}
}
public class Demo2 {
public static void main(String[] args) {
//2. 创建实现类的对象
// A a = new AImpl(); //多态的向上转型
//3. 通过实现类的对象调用你show方法
// a.show();
/*
解决方案二:
1. 使用匿名内部类得到接口A的对象
2. 调用show方法
*/
// new A() {
//
// @Override
// public void show() {
// System.out.println("匿名内部类的方式调用show方法");
// }
// }.show();
A a = new A() {
@Override
public void show() {
System.out.println("匿名内部类的方式调用show方法");
}
};
a.show();
}
}
练习:
定义三个类:
1. Person类中有一个抽象方法eat
2. PersonTest类中有一个test(Person p)方法,test方法中调用了Person类中的eat方法
3. Demo类为测试类
要求在测试中通过调用test方法得到想吃的东西
/*
定义三个类:
1. Person类中有一个抽象方法eat
2. PersonTest类中有一个test(Person p)方法,test方法中调用了Person类中的eat方法
3. Demo类为测试类
要求在测试中通过调用test方法得到想吃的东西
*/
/*
Person类
*/
abstract class Person {
public abstract void eat();
}
/*
PersonTest类
*/
class PersonTest {
public void test(Person p){
//参数需要的是类类型,实际上需要的是该类的对象
p.eat();
}
}
/*
方式一:Person的子类
*/
class PP extends Person {
@Override
public void eat() {
System.out.println("吃饭!");
}
}
/*
测试类
*/
public class Demo3 {
public static void main(String[] args) {
//1. 创建PersonTest对象
PersonTest pt = new PersonTest();
//2. 通过pt调用test方法
//子类方式
// PP p = new PP();
// pt.test(p); //向上转型
//方式二:匿名内部类方式
// Person p = new Person(){
//
// @Override
// public void eat() {
// System.out.println("吃面!");
// }
// };
// pt.test(p);
pt.test(new Person(){
@Override
public void eat() {
System.out.println("吃面!");
}
});
}
}
Three, anonymous objects
Objects without names such as: new Person();
3.1 When to use
1. 在只调用一次方法的时候
2. 当做参数的时候使用
class Person {
public void eat(){
System.out.println("吃饭");
}
}
class PersonTest {
public void test(Person p){
p.eat();
}
}
public class Demo {
public static void main(String[] args) {
//1. 创建对象
// Person p = new Person();
//2. 通过对象名调用eat方法
// p.eat();
// p.eat();
// p.eat();
//通过匿名对象调用
// new Person().eat();
// new Person().eat();
// new Person().eat();
// PersonTest pt = new PersonTest();
// pt.test(new Person());
new PersonTest().test(new Person());
}
}