JavaSE——面向对象(六)(方法参数类型及返回值类型,内部类,匿名内部类)

1.方法参数类型及返回值类型

  • 概述

有没有想过这样一个问题,当方法的形式参数是一个类名,抽象类名,接口名。以及方法的返回值类型是类类型,抽象类类型,接口类型是怎么办?

  • 形参为类名,抽象类名,接口名时(案例演示)
//形参为类名时
package com.westmo.demo4;
public class MyTest {
    public static void main(String[] args) {
        AA aa = new AA();
        test(aa);//传一个该类的对象
    }
    public static void test(AA aa){ //当形参是类名时
        aa.show();
    }
}
class AA{
    int num;
    public void show(){
        System.out.println("123");
    }
}
//当形参为抽象类名时
package com.westmo.demo4;
public class MyTest1 {
    public static void main(String[] args) {
        Test1 test1 = new Test1();
        test(test1);/传入该抽象类的子类对象
    }
    public static void test(Test test ){  //形参为一个抽象类名时
        test.show();
    }
}
abstract  class Test{
    public abstract void show();
}
class Test1 extends Test{
    @Override
    public void show() {
        System.out.println("123");
    }
}
//接口名作为形参时,也是传它子类的一个对象,和抽象类很像
//当方法的返回值类型时类型时
package com.westmo.demo4;
public class MyTest2 {
    public static void main(String[] args) {
        Student1 st = new Student1();
        Student1 student2=test(st,"张三",30);
        System.out.println(st.name+" "+st.age);
        System.out.println(student2.name+" "+student2.age);
    }
    public static Student1 test(Student1 student1, String name,int age){//返回类型是Student1类型
        student1.name=name;
        student1.age=age;
        Student1 student2 = new Student1();
        return student2;//返回该类的一个对象
    }
}
class Student1{
    String name="李四";
    int age=20;
    public Student1() {
        this.name = name;
        this.age=age;
    }
    public String getName() {
        return name;
    }
    public int getAge(){
        return age;
    }
}
//当方法的返回值类型是抽象类型时
package com.westmo.demo4;

public class MyTest3 {
    public static void main(String[] args) {
        People people=test(new Children(),50);
        people.show(10);

    }
    public static People test(People people,int num){ //返回值类型是抽象类类型
        people.num=num;
        Children children = new Children();
        return children;//返回它子类的对象
    }
}
abstract class People{
    int num=20;
    public abstract void show(int num);
}
class Children extends People{
    @Override
    public void show(int num) {
        System.out.println(this.num);
        System.out.println(super.num);
    }
}
//方法的返回值类型为接口类型时,与抽象类一样,返回该接口的一个子类对象即可。

2.内部类

  • 概述

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

  • 内部类访问特点

内部类可以直接访问外部类的成员,包括私有的。
外部类要想直接访问内部类的成员,逆对象来访问。

package com.westmo.demo4;
public class MyTest5 {
    public static void main(String[] args) {
        waibu.neibu neibu = new waibu().new neibu();//在外界创建内部类的成员
        neibu.neishow();
    }
}
class waibu{
    String name="你好";
    private int age=20;
    public void show(){
        System.out.println("外部类");
    }
    class neibu{
        int num=19;
        String name="qwe";
        public void neishow(){
            System.out.println(age);//内部类中可以直接访问外部类中的成员
            show();
        }
    }
    public void test(){
        neibu neibu = new neibu();
        System.out.println(neibu.name);
        neibu.neishow(); //外部类访问内部类需要创建对象(在外部类中)
    }
}
  • 内部类的分类

按照内部类的位置:在成员位置定义的类称为成员内部类,在局部位置定义的类称为局部内部类。

//成员内部类案例演示
package com.westmo.demo4;

public class MyTest6 {
    public static void main(String[] args) {
        WB.NB nb = new WB().new NB(); //访问方式
        nb.show();
        //System.out.println(nb.s);//一旦内部类的成员私有,外界就不能直接创建内部类的对象类访问
    }
}
class WB{
    int num=10;
    class NB{
        int num=20;
        private int s=10;
        public void show(){
            int num=30;
            System.out.println(num);
            System.out.println(NB.this.num);
            System.out.println(WB.this.num);
        }
    }
}
  • 成员内部类注意事项

静态内部类访问的外部数据必须是静态修饰的
成员方法可以是静态的,也可以是非静态的。
成员内部类被静态修饰后的访问方式是:外部类名.内部类名 对象名=new 外部类名.内部类名();

//局部内部类(在外部类的方法中)
//外界无法直接创建对象
package com.westmo.demo4;

public class MyTest7 {
    public static void main(String[] args) {
        Outer outer = new Outer();
        outer.waibu();
    }
}
class Outer{
    int age=20;
    private double t=10;
    public void waibu(){
        class Inner{
            int s=30;
            public void neibu(){
                System.out.println(t);
            }
        }
        //创建内部类对象
        Inner inner = new Inner();
        inner.neibu();
    }
}
  • 局部内部类注意事项

局部内部类访问局部变量必须用final修饰,因为局部变量会随着方法的调用完毕而消失,这个时候,局部对象并没有立马从堆内存中消失,还要使用那个变量,为了让数据还能被继续使用,就是用final修饰,这样,在堆内存里面存储的其实就是一个常量值。

3.匿名内部类

  • 概述:

就是局部内部类的简化写法

  • 前提条件

存在一个具体类或抽象类,接口,

  • 本质

就是一个子类的匿名对象(该子类继承了某个类或者实现了某个接口),里面是重写父类中的方法。

package com.westmo.demo4;
public class MyTest8 {
    public static void main(String[] args) {
        new Person(){
            @Override
            public void eat() {
                System.out.println("吃饭");
            }
            @Override
            public void sleep() {
                System.out.println("睡觉");
            }
        }.eat();//吃饭
    }
}
abstract class Person{
    public abstract void eat();
    public abstract void sleep();
}
class student extends Person{
    @Override
    public void eat() {
        System.out.println("吃米饭");
    }
    @Override
    public void sleep() {
        System.out.println("晚上睡觉");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值