Java method parameters, return values types of applications, permissions modifier, inner classes, anonymous inner class overview.

Method parameter types, and return type overview and demonstration

Class name as a formal parameter demo

public class MyTest {
    public static void main(String[] args) {
        Student student = new Student();
        student.show(student, 7);//如果你以后看到一个方法的形参需要一个类,类型你就传该类对象
        //如果将上面student对象换成匿名对象student()那莫下面就会输出100因为匿名对象是一个新对象你把新对象的传入下面那莫会开辟新的空间赋值,而输出的还是上面的student,因为上面的student就没有赋值所以还是100,如果将下面的a.num换为this.num那莫就会输出7,因为this谁调用代表谁,是上面的student调用那莫this就代表上面的student,给上面的student重新赋值。而我们直接用的上面的对象调用也就是说都是一个对象所以输出7
        System.out.println(student.num);
    }
}

class Student {
    int num=100;
    public void show (Student a,int num){

        a.num=num;
    }
}

Abstract class name as a formal parameter demo

public class MyTest2 {
    public static void main(String[] args) {
        //如果你以后看到一个方法的形参要一个 抽象类 类型,你就传一个该类的子类对象
        Dog dog = new Dog();
        setMethod(dog,114);

    }

    private static void setMethod(Animal an,int num) {

        an.show(num);

    }


}

abstract class Animal{
    int num=110;
    public abstract void show(int a);
}

class Dog extends Animal{
    int num=20;

    @Override
    public void show(int num) {
        System.out.println(num);//114
        System.out.println(this.num);//20this访问本类成员变量
        System.out.println(super.num);110super访问父类成员变量
    }
}

Interface name as a formal parameter demo

public class MyTest3 {
    public static void main(String[] args) {
        //如果你以后看到一个方法的形参要一个 接口 类型 你就传一个该接口的子类对象
        MyClass myClass = new MyClass();
        test(myClass,22);
    }

    private static void test(MyInterface myInterface,int b) {
        myInterface.show(b);
    }
}

interface MyInterface{
    int NUM=100;
    void show(int a);

}

class MyClass implements MyInterface{
    int b=10;
    @Override
    public void show(int a) {
        System.out.println(this.b);
        System.out.println(NUM);
        System.out.println(MyInterface.NUM);
    }
}

Class name as the return value type presentation

public class MyTest {
    public static void main(String[] args) {
        //如果你以后看到一个方法的返回值类型,要一个类 类型 你就返回一个该类对象
        Student student = new Student();
        student.num=20;
        Student student1 = student.getStudent(new Student(),111);

        System.out.println(student1.num);

        System.out.println(student1==student);

    }
}

class Student {
    int num=10;

    public Student getStudent(Student student,int num){
        this.num=num;
        //Student student2 = new Student();
        return this;
    }

}

Abstract class name as the return value type presentation

public class MyTest3 {
            public static void main(String[] args) {
                Animal animal = getAnimal(new Cat());
                animal.test();
            }
            //如果你以后看到一个方法的返回值类型,要一个抽象类 类型,你返回一个该抽象类的子类对象
            private static Animal getAnimal(Cat cat) {


                return cat;

    }
}


abstract class Animal{
    public abstract void test();

}

class Cat extends Animal{

    @Override
    public void test() {
        System.out.println("abc");
    }
}

Interface name as the return value type presentation

public class MyTest2 {
    public static void main(String[] args) {
        MyInterface anInterface = getInterface(10);
        anInterface.show(1000);
    }
        //如果你以后看到一个方法的返回值类型,要一个 接口类型,你就返回一个该接口的子类对象
    private static MyInterface getInterface(int num) {
        num=11;


        return new MyClass();
    }
}

interface  MyInterface{

    void show(int num);
}


class MyClass implements  MyInterface{

    @Override
    public void show(int num) {
        System.out.println(num);
    }
}

Chain equation

public class MyTest {
    public static void main(String[] args) {
        //链式编程 当你调用完一个方法后,他的返回值又是个对象,那么你紧接着,就可以打点,在继续调用对象中的方法
        Student student = new Student();
        int i = student.getStudent(new Student()).show(10);
        System.out.println(i);
       //把上面的拆开来写
       // Student student = new Student();
       // Student student1 = student.getStudent(new Student());
       // int i1 = student1.show(10);

        System.out.println(student.num);



    }
}

class Student {
    int num = 10;

    public Student getStudent(Student student) {
        student.num = 35;


        return student;

    }

    public int show(int num) {
        this.num = num;
        return num+=10;
    }

}

Permissions modifier

Qualifier This class Under the same package (regardless of category subclass) Different under-cladding (subclass) Under different package (regardless of type)
private Y
default Y Y
protected Y Y Y
public Y Y Y Y

Common modifier class and its components used

Modifiers
  1. Permissions modifiers: private, default, protected, public
  2. State modifiers: static, final
  3. Abstract modifier: abstract
Modified class keyword
  1. Permissions modifiers: default modifier, public
  2. State modifiers: final
  3. Abstract modifier: abstract
  4. With the most is: public
Modified member variable keywords
  1. Permissions modifiers: private, default, protected, public
  2. State modifiers: static, final
  3. With the most is: private
Modified constructor keyword
  1. Permissions modifiers: private, default, protected, public
  2. With the most is: public
Keywords for member modification method
  1. Permissions modifiers: private, default, protected, public
  2. State modifiers: static, final
  3. Abstract modifier: abstract
  4. With the most is: public
In addition to the combination rule
  1. Member variable: public static final
  2. Member method: public static, public abstract. public final

Inner classes

Overview:

The class is defined inside other classes, this class has become inner classes

For example

We define a class A, class B, class B is the internal type.

Features like internal access
  • You can directly access the internal class members outside of class, including private.
  • To access the outer class members within the class, you must create the object.
Direct use of internal class classification and internal members of the class
  • Member location: class member in a defined position, is referred to as internal member class.
  • Localized positions: a position in the local class definition, is called internal local class.

If you are calling an internal class in the classification you need new inner class object format: External class name inside the class name object name = external object inside the class object;

Case presentation
public class Outer {
    int num=10;

    private int a=20;
    //定义一个成员内部类
    class Inner{
        int aa=1;
        public void innnerShow(){
            System.out.println("内部类的show方法");
            System.out.println(num);
            System.out.println(a);
            waiShow();
            waiTest();
        }

    }

    public void waiShow(){
        System.out.println("这是外部类的方法");
    }

    private void waiTest(){
        System.out.println("外部类的私有方法");
    }

    public void test(){
        //外部类想要访问内部类的成员,得创建内部类的对象
        Inner inner = new Inner();
        System.out.println(inner.aa);
    }

}
------------------------------------------
public class MyTest {
    public static void main(String[] args) {
        //调用内部类的方法,要创建内部类的对象
        Outer.Inner inner=new Outer().new Inner();
        inner.innnerShow();
        //内部类的特点:内部类可以直接访问外部类的成员,包括私有的成员

        //外部类想要调用内部类的成员,得创建内部类对象


    }
}

You can not create a private inner class outside its objects

public class MyTest {
    public static void main(String[] args) {
        //私有的内部类,外界不能创建其对象了
        //Wai.Nei nei = new Wai().new Nei();
        int show = new Wai().show();
        System.out.println(show);
        new Wai().hehe();

    }
}


class Wai {
    //内部类可以用private修饰
    private class Nei {
        int a = 100;
        public void test(){
            System.out.println("内部类的方法");
        }
    }

    public int show() {
        Nei nei = new Nei();
        return nei.a;
    }
    public void hehe(){
        Nei nei = new Nei();
        nei.test();
    }
}
Common modifier members of the inner class
  • In order to ensure the security of private data
  • In order to facilitate access to static data
    PS : external static inner classes class data access must be static modification. Members of the method can be static or a non-static
public class MyTest2 {
    public static void main(String[] args) {
       //未用staitc修饰的内部类的创建对象的语法
       // WaiBu.NeiBu neiBu = new WaiBu().new NeiBu();

        //static 修饰内部类,那么创建内部类对象的语法,更简洁了
        //静态内部类,只能访问外部类的静态成员
        WaiBu.NeiBu neiBu=new WaiBu.NeiBu();

    }

}

class WaiBu{
    static int wai=111;
     static class NeiBu{//static 可以修饰内部类型
        int num=10;
        public void neiShow(){
            System.out.println(wai);
            waiShow2();
        }
    }

    public void waiShow(){

    }

    public static  void waiShow2() {

    }

}
Members of the inner class presentation
public class MyTest {
    public static void main(String[] args) {
        //内部类可以直接访问外部类的成员,包括私有的。
        //局部内部类,在外界没有直接创建其对象语法
        Outer outer = new Outer();
        outer.waiShow();

    }
}


class Outer{
    int num=100;
    private int a=10;
    public void waiShow(){
        //局部位置
        class Inner{ //局部内部类
            int aa=2;
            public void neiShow(){
                System.out.println("内部类的show方法");
                System.out.println(num);
                System.out.println(a);
                hehe();
            }

        }

        Inner inner = new Inner();
        inner.neiShow();

    }

    private void hehe(){

    }

    public void waiTest(){


    }

}
The problem of local inner class access local variables
public class MyTest2 {
    public static void main(String[] args) {

    }
}

class Wai{

    public void waiShow(final int num){
      final int a=1; //外部类的局部变量
        //定义局部内部类
        //在局部内部类里面,要访问外部类的局部变量,那这个局部变量,要加上final 让他成为一个常量,JDK1.8之后默认就加上了;
        //为什么要加 final
        class Nei{
            public void neiShow(){
               // a=10;
                System.out.println(a);
                System.out.println(num);
            }

        }

    }
}

Anonymous inner classes

premise:

There is a class or interface; herein may be a particular class may be abstract class. Anonymous inner classes: is to simplify the wording of local inner class.
Anonymous inner classes: it is a shorthand way of local inner class of
an object on an anonymous inner class nature, whose object is to achieve the interface or subclass object inherits the abstract class is
a prerequisite for anonymous inner classes: To there is an interface or abstract class (general class too)

format:

new name of the class or interface name () {
override method;
};

Nature:

Is an inherited class or subclass that implements this interface anonymous object.

Interface anonymous inner class presentation
public class MyTest2 {
    public static void main(String[] args) {

new MyInterface(){
    @Override
    public void show() {
        System.out.println("hehhehe");
    }
}.show();
}
}
interface MyInterface{
    void show();
}
Abstract anonymous inner classes demo
public class MyTest2 {
    public static void main(String[] args) {
    new Animal(){
    @Override
    public void eat() {
        System.out.println("吃鸡");
    }
};
abstract class Animal{
    public abstract void eat();
}
Anonymous inner classes can call one or more methods
public class MyTest3 {
    public static void main(String[] args) {
        new MyInterface(){//第一个匿名内部类调用
            @Override
            public void show() {
                System.out.println("show");
            }

            @Override
            public void test() {
                System.out.println("test");
            }
        }.show();

        new MyInterface() {//第二个匿名内部类对象调用
            @Override
            public void show() {
                System.out.println("show");
            }

            @Override
            public void test() {
                System.out.println("test");
            }
        }.show();


        new MyInterface() {//第三个匿名内部类对象调用
            @Override
            public void show() {
                System.out.println("show");
            }

            @Override
            public void test() {
                System.out.println("test");
            }
        }.test();
        //以上每调用一次就重新改写一次方法,是不同的匿名内部类对象调用

 MyInterface myInterface= new MyInterface(){//第二种方法,这个方法是调用一个抽象类的两个方法。没有新new的对象只new了一次

            @Override
            public void show() {
                System.out.println("show222");
            }

            @Override
            public void test() {
                System.out.println("test2222");
            }
        };

        myInterface.test();
        myInterface.show();
        myInterface.show();


    }
}

interface MyInterface{
    void show();
    void test();

}
  
Anonymous inner class methods call 1
public class MyTest {
   public static void main(String[] args) {
       setInterface(new MyInterface() {
           @Override
           public void show() {
               System.out.println("hahahahahhaha");
           }
       });

       MyInterface myInterface = new MyInterface() {
           @Override
           public void show() {
               System.out.println("hahahahahhaha222222");
           }
       };
       setInterface(myInterface);
   }

   private static void setInterface(MyInterface myInterface) {
       myInterface.show();
   }

}

interface MyInterface {
   void show();
}

Method Invocation two

public class MyTest2 {

    public static void main(String[] args) {
      Animal an=  getAnimal();
      an.show();
    }

    private static Animal getAnimal() {


        return new Animal() {
            @Override
            public void show() {
                System.out.println("abc");
            }
        };
    }
}

abstract class Animal {
    public abstract void show();

}

Requirements output in the console "HelloWorld"

interface Inter2 {
   public abstract void show();
}

class Outer {
    //补全代码
    public static Inter2 method(){

        return new Inter2() {
            @Override
            public void show() {
                System.out.println("hellworld");
            }
        };
    };
}

class OuterDemo {
    public static void main(String[] args) {
        // 要求在控制台输出”HelloWorld”
        Outer.method().show();
    }
}

How to call method method PersonDemo in it?

abstract class Person {
    public abstract void show();
}



class PersonDemo {
    public void method(Person p) {
        p.show();
    }
}

class PersonTest {
    public static void main(String[] args) {
        //如何调用PersonDemo中的method方法呢?
      new PersonDemo().method(new Person() {
            @Override
            public void show() {
                System.out.println("abdfdfd");
            }
        });

    }
}
Application of this anonymous inner class
interface Inter {
   public static final int a = 23;
}

public class Test {

      public static void main(String[] args) {
         new Inter() {
            public void show() {
            // 这个this表示的是匿名内部类的这个对象
               System.out.println(this.a);//23
               System.out.println(Inter.a);
            }
         }.show();
      }
   }
How to define an excuse in class
public class MyTest {
    public static void main(String[] args) {
        //可以在类中定义一个内部接口
        //方式1
        new Outer().waiShow();

        //方式2
        Outer.MyInterface myInterface = new Outer.MyInterface() {

            @Override
            public void show() {
                System.out.println("hehehhehe");
            }
        };

        myInterface.show();

    }
}

class Outer{

    //定义成员内部接口
    interface MyInterface{
        void show();
    }

    public void waiShow(){
        new MyInterface(){

            @Override
            public void show() {
                System.out.println("abc");
            }
        }.show();

    }

}

Guess you like

Origin blog.csdn.net/weixin_45111939/article/details/93642369