Java中方法参数,返回值类型的应用,权限修饰符,内部类,匿名内部类概述。

方法参数类型,及返回值类型概述及演示

类名作为形式参数演示

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;
    }
}

抽象类名作为形式参数演示

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访问父类成员变量
    }
}

接口名作为形式参数演示

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);
    }
}

类名作为返回值类型演示

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;
    }

}

抽象类名作为返回值类型演示

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");
    }
}

接口名作为返回值类型演示

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);
    }
}

链式方程

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;
    }

}

权限修饰符

修饰词 本类 同一个包下(子类无关类) 不同包下(子类) 不同包下(无关类)
private Y
默认 Y Y
protected Y Y Y
public Y Y Y Y

类及其组成所使用的常见修饰符

修饰符
  1. 权限修饰符:private,默认的,protected,public
  2. 状态修饰符:static,final
  3. 抽象修饰符:abstract
修饰类关键字
  1. 权限修饰符:默认修饰符,public
  2. 状态修饰符:final
  3. 抽象修饰符:abstract
  4. 用的最多的就是:public
修饰成员变量的关键字
  1. 权限修饰符:private,默认的,protected,public
  2. 状态修饰符:static,final
  3. 用的最多的就是:private
修饰构造方法关键字
  1. 权限修饰符:private,默认的,protected,public
  2. 用的最多的就是:public
修饰成员方法的关键字
  1. 权限修饰符:private,默认的,protected,public
  2. 状态修饰符:static,final
  3. 抽象修饰符:abstract
  4. 用的最多的就是:public
除此以外的组合规则
  1. 成员变量:public static final
  2. 成员方法:public static,public abstract。public final

内部类

概述:

把类定义在其他类的内部,这个类就成为内部类

举例

在类A中定义了一个类B,类B就是内部类。

内部类访问特点
  • 内部类可以直接访问外部类的成员,包括私有。
  • 外部类要访问内部类的成员,必须创建对象。
内部类分类及成员内部类的直接使用
  • 成员位置:在成员位置定义的类,被称为成员内部类。
  • 局部位置:在局部位置定义的类,被称为局部内部类。

如果要在分类中调用内部类你需要new内部类对象,格式: 外部类名.内部类名 对象名 = 外部类对象.内部类对象;

案例演示
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();
        //内部类的特点:内部类可以直接访问外部类的成员,包括私有的成员

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


    }
}

私有内部类外界不能创建其对象

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();
    }
}
成员内部类的常见修饰符
  • private 为了保证数据的安全性
  • static 为了方便访问数据
    ps:静态内部类访问的外部类数据必须用静态修饰。成员方法可以是静态的也可以是非静态的
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() {

    }

}
成员内部类演示
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(){


    }

}
局部内部类访问局部变量的问题
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);
            }

        }

    }
}

匿名内部类

前提:

存在一个类或者接口;这里的类可以是具体类也可以是抽象类。匿名内部类: 就是局部内部类的简化写法。
匿名内部类:是局部内部类的一种简写方式
匿名内部类本质上是一个对象,是谁的对象,是实现了该接口或继承了该抽象类的子类对象
匿名内部类的前提条件:要有一个接口或者抽象类(普通的类的也行)

格式:

new 类名或者接口名(){
重写方法;
} ;

本质:

是一个继承了该类或者实现了该接口的子类匿名对象。

接口匿名内部类演示
public class MyTest2 {
    public static void main(String[] args) {

new MyInterface(){
    @Override
    public void show() {
        System.out.println("hehhehe");
    }
}.show();
}
}
interface MyInterface{
    void show();
}
抽象类匿名内部类演示
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();
}
匿名内部类可以调用一个或多个方法
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();

}
  
匿名内部类方法调用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();
}

方法调用二

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();

}

要求在控制台输出”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();
    }
}

如何调用PersonDemo中的method方法呢?

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");
            }
        });

    }
}
this 在匿名内部类的应用
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();
      }
   }
如何在类中定义借口
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();

    }

}

猜你喜欢

转载自blog.csdn.net/weixin_45111939/article/details/93642369