JAVA面向对象之内部类与方法参数类型及返回值类型

JAVA面向对象之内部类与方法参数类型及返回值类型

1.内部类

内部类将一个类A定义到另一个类B的内部,那么类A叫做,内部类类B叫做外部类根据内部类在外部类中定义的位置不同,分为成员内部类和局部内部类;

成员内部类:定义在外部类的成员位置

局部内部类:定义在外部类的局部位置,也就是外部类中的方法内部

public class MyTest {
    public static void main(String[] args) {
   
    }
}

class B {
    //成员内部类
    class A {

    }

    public void show() {
        //局部内部类
        class C {

        }
    }

}

1.2创建成员内部类的对象语法:

外部类名.内部类名 对像名=外部类对象.内部类对象

外部类不可私有修饰

public class Wai {
    int num = 20;
    private int a = 60;

    //成员内部类
    public class Nei {
        int n = 5;
        int x = 100;

        public void neiShow() {
            System.out.println("内部类的show方法" + n);
            //内部类的特点:内部类可以直接访问外部类的成员,包括私有成员。
            System.out.println(num);
            System.out.println(a);
            waiShow();
            hehe();
        }

        public void neiHeHe() {
            System.out.println("内部类hehe方法");
        }
    }

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

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

    public void test() {
        //创建内部类对象
        Nei nei = new Nei();
        System.out.println(nei.n);
        System.out.println(nei.x);
        nei.neiHeHe();
    }
}

public class MyTest2 {
    public static void main(String[] args) {
        //在测试类里面,调用内部类的方法
        //创建成员内部类的对象的语法如下:格式: 	外部类名.内部类名 对象名 = 外部类对象.内部类对象;
        Wai.Nei nei = new Wai().new Nei();
        nei.neiShow();
        System.out.println("====================================");
        //内部类的特点:内部类可以直接访问外部类的成员,包括私有成员。

        Wai wai = new Wai();
        wai.test();
    }
}

/*输出
内部类的show方法5
20
60
外部类的show方法
外部类的私有方法
====================================
5
100
内部类hehe方法
*/

1.3内部类可以私有修饰,私有的成员内部类,外界就无法创建其对象

public class MyTest {
    public static void main(String[] args) {
        //私有的成员内部类,外界就无法创建其对象了
        //Outer.Inner inner = new Outer().new Inner();
        //inner.neiShow();

        Outer outer = new Outer();
        outer.waiShow();
    }
}

class Outer {
    //内部类可以私有修饰
    private class Inner {
        public void neiShow() {
            System.out.println("nei show");
        }
    }

    public void waiShow() {
        Inner inner = new Inner();
        inner.neiShow();
    }

1.3static也可以修饰内部类

静态的成员内部类 创建语法更加简介:Wai.Nei nei=new wai.nei()

静态的成员内部类,只能访问外部类的静态成员

public class Wai {
    static int num = 100;

    //static 也可以修饰内部类
    public static class Nei {
        public void neiShow() {
            //静态的成员内部类,只能访问外部类的静态成员。
            System.out.println(num);
        }
    }
    
    
public class MyTest {
    public static void main(String[] args) {
        //静态的成员内部类,创建对象的语法
        Wai.Nei nei = new Wai.Nei();
    }
}

1.4访问 外部类名.this.num

class InnerClassTest {
    public static void main(String[] args) {
        Outer.Inner oi = new Outer().new Inner();
        oi.show();
        //变量访问遵循就近原则:
    }
}

class Outer {
    //外部类的成员变量
    public int num = 10;

    class Inner {
        //内部的成员变量
        public int num = 20;

        public void show() {
            //局部变量
            int num = 30;
            System.out.println(num); //30
            System.out.println(this.num); //20

            System.out.println(Inner.this.num); //20
            //这样太繁琐了
            //System.out.println(new Outer().num); //10
            // 这个语法 Outer.this 表示外部类对象
            System.out.println(Outer.this.num);
            	}
         }
}

1.5局部内部类,要访问外部类的局部变量,必须用final修饰,使之成为常量

public class MyTest2 {

}

class Outer {
    public void waiShow(final int a) {
        //局部变量
        final int num = 20; //JDK1.8 之后,这个final默认就加上了,JDK1.8之前必须手动加上。

        /*
        *    为什么呢?
  	因为局部变量会随着方法的调用完毕而消失,这个时候,局部对象并没有立马从堆内存中消失,还要使用那个变量。
	为了让数据还能继续被使用,就用final修饰,这样,在堆内存里面存储的其实是一个常量值。
	JDK1.8之后,final会默认加上,你不用手动去加,但是你要知道
        * */
        //定义局部内部类
        class Inner {
            public void neiShow() {
                //Variable 'num' is accessed from within inner class, needs to be final or effectively final
                //局部内部类,要访问外部类的局部变量,局部变量,必须用final修饰,使之成为常量
                //num = 120; 报错,他是常量
                System.out.println(num);
                System.out.println(a);
            }
        }
    }
}

1.6外部类访问内部类里面的成员方法

需要在外部类的方法中,创建内部类对象,然后用对象去调用

public class MyTest2 {
    public static void main(String[] args) {
        Wai wai = new Wai();
        wai.test();
    }
}


public class Wai {
    int num = 20;
    private int a = 60;

    //成员内部类
    public class Nei {
        int n = 5;
        int x = 100;

        public void neiShow() {
            System.out.println("内部类的show方法" + n);
            //内部类的特点:内部类可以直接访问外部类的成员,包括私有成员。
            System.out.println(num);
            System.out.println(a);
            waiShow();
            hehe();
        }

        public void neiHeHe() {
            System.out.println("内部类hehe方法");
        }
    }

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

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

    public void test() {
        //创建内部类对象
        Nei nei = new Nei();
        System.out.println(nei.n);
        System.out.println(nei.x);
        nei.neiHeHe();
    }
}

2.面向对象之(方法参数类型以及返回值类型)

  • 类名作为形参参数
//当你以后看到一个方法的形参,要一个类 类型,就传递一该类的参数
public class MyTest {
    public static void main(String[] args) {
        int num = 20;
        Student student = new Student();
        student.num = 50;
         //基本类型作为参数传递:属于值传递,传递的是值,形参的改变,不影响实参。
        //引用类型作为参数传递:属于引用传递,传递的是地址值,形参的改变会影响实参。
        test(student, num);//20
        student.show(new Student(), 80);
        System.out.println(student.num); //20
    }

    public static void test(Student student, int num) {
        student.num = num;
    }
}

class Student {
    int num = 100;

    public void show(Student studnet, int num) {
        studnet.num = num;
    }
}
  • 抽象类名作为形式参数
 //当你以后看到一个方法的形参,要一个 抽象类 类型,你就传递一个该抽象类的子类对象。 说白了,就是多态
public class MyTest {
    public static void main(String[] args) {
      
        //多态形式
        AA aa = new BB();
        aa.num = 88;
        test(aa, 2);
        System.out.println(aa.num); //2
    }

    public static void test(AA aa, int num) { //AA aa=bb 多态
        //多态形式访问成员变量,编译看左边,运行也看左边。
        aa.num = num;
    }
}

abstract class AA {
    int num = 100;

    public abstract void aa(int num);
}

class BB extends AA {
    int num = 30;

    @Override
    public void aa(int num) {
        this.num = num;
    }
}

//#####################################################################################
public class MyTest1 {
    public static void main(String[] args) {
      
        //多态形式
       BB bb = new BB();
        aa.num = 88;
        test(bb, 2);
        System.out.println(bb.num); //88
    }

    public static void test(AA1 aa, int num) { //AA aa=bb 多态
        //多态形式访问成员变量,编译看左边,运行也看左边。
        aa.num = num;
    }
}

abstract class AA1 {
    int num = 100;

    public abstract void aa(int num);
}

class BB extends AA1 {
    int num = 30;

    @Override
    public void aa(int num) {
        this.num = num;
    }
}
  • 接口名作为形式参数
  //当你以后看到一个方法的形参要一个 接口 类型,你就传递一个该接口的实现类的对象。
public class MyTest {
    public static void main(String[] args) {
        DD dd = new DD();
        test(dd);
        System.out.println(dd.num);
        System.out.println(MyInterface.num);
    }

    public static void test(MyInterface myInterface) { //  MyInterface myInterface = dd  多态
        System.out.println(myInterface.num);
    }
}

interface MyInterface {
    public static final int num = 100;

    void show();
}

class DD implements MyInterface {
    @Override
    public void show() {
        System.out.println(this.num);
    }
}

  • 类名作为返回值
//如果你以后看到一个方法的返回值类型,是一个类 类型,你就返回该类的对象。
public class MyTest {
    public static void main(String[] args) {
        Student student = getStudent(10);
        student.num = 555;
        Student s2= student.test(89);
        System.out.println(student.num); //89
        System.out.println(s2.num); //89
 
        System.out.println(s2==student);
    }
    public static Student getStudent(int num) {
        Student student = new Student();
        student.num = num;
        return student;
    }
}
class Student {
    int num = 20;
    public Student test(int a) {
        this.num = a;
        return this; //this 代表一个该类的引用,哪个对象调用这个方法,方法中的this 就代表谁
    }


  • 抽象类名作为返回值类型
//如果以后你看到一个方法的返回值类型,要一个抽象类 类型,你就返回该抽象类的子类对象。
public class MyTest {
    public static void main(String[] args) {
        AA aa = test();
        //多态形式,访问成员变量,编译看左边,运行也看左边。
        System.out.println(aa.num);//200
        //向下转型
        BB bb = (BB) aa;
        System.out.println(bb.num); //77
    }

    public static AA test() {
        BB bb = new BB();
        bb.num = 77;
        return bb;
    }
}

abstract class AA {
    int num = 200;

    public abstract void aa();
}

class BB extends AA {
    int num = 500;

    @Override
    public void aa() {
        this.num = 90;
    }
}

  • 接口名作为返回值类型
//如果你以后看到一个方法的返回值类型,要一个接口类型,你就返回一个该接口的实现类对象。
public class MyTest {

    public static void main(String[] args) {
        MyInterface myInterface = test();  // MyInterface myInterface=ee 多态
        System.out.println(myInterface.num);//100

        //向下转型。
        EE ee = (EE) myInterface;
        System.out.println(ee.num); //88


    }

    public static MyInterface test() {
        EE ee = new EE();
        ee.num = 88;
        return ee;
    }
}

interface MyInterface {
    int num = 100;

}

class EE implements MyInterface {
    int num = 600;
}

总结: ①当你以后看到一个方法的形参,要一个类 类型,就传递一该类的参数

​ ②当你以后看到一个方法的形参,要一个 抽象类 类型,你就传递一个该抽象类的子类对象。 说白了,就是多态

​ ③当你以后看到一个方法的形参要一个 接口 类型,你就传递一个该接口的实现类的对象。

​ ④如果你以后看到一个方法的返回值类型,是一个类 类型,你就返回该类的对象。

​ ⑤如果以后你看到一个方法的返回值类型,要一个抽象类 类型,你就返回该抽象类的子类对象。

​ ⑥如果你以后看到一个方法的返回值类型,要一个接口类型,你就返回一个该接口的实现类对象。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值