JAVASE基础模块十一(传参与返回值 链式编程 权限修饰符 关键字 import package)

JAVASE基础模块十一(传参与返回值 链式编程 权限修饰符 关键字 import package)

传参与返回值

  • 当你看到一个方法的形参要一个抽象类 类型 你就传递一个该类的子类对象
public class X1 {
    public static void main(String[] args) {

        Student s = new Student();
        set(s, 60);
        s.show(new Student(), 80);
        System.out.println(s.num);

    }

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

class Student {
    int num = 33;

    public void show(Student student, int num) {
        student.num = num;
    }
}
运行结果:
60

Process finished with exit code 0
无返回值的传参
public class x2 {
    public static void main(String[] args) {
        Cat cat = new Cat();
        set(cat,1000);
        cat.show(5555);
        System.out.println(cat.num);

    }
    public static void set(Animal animal,int num) {
      animal.num=num;
        ((Cat)animal).num=num;
    }
}
abstract class Animal{
    int num=50;
    public abstract void show(int num);
}
class Cat extends Animal {
    int num=60;

    @Override
    public void show(int num) {
        this.num = num;
    }
}
运行结果:
5555

Process finished with exit code 0
  • 当你看到一个方法的形参要一个接口类 类型 你就传递一个该接口的子类对象
public class C1 {
    public static void main(String[] args) {
        Haaa haaa = new Haaa();
        set(haaa,555);
        System.out.println(haaa.num);
        System.out.println(Plus.num);


    }
    public static void set(Plus plus,int num) {
        plus.show(num);
    }
}
interface Plus{
    int num=333;
    void show(int num);
}
class Haaa implements Plus{
    int num=2;

    @Override
    public void show(int num) {
        this.num = num;
    }
}
运行结果:
555
333

Process finished with exit code 0
  • 当你以后看到一个方法的返回值类型是一个抽象类 类型,你就返回一个该抽象类的子类对象
public class C2 {
    public static void main(String[] args) {
        Animal animal = getAnimal();
        animal.num=777;
        System.out.println(animal.num);
        //向下转型Dog dog=(dog)getAnimal()

    }
    public static Animal getAnimal() {
        Dog dog = new Dog();
        dog.num=70;
        return dog;
    }

}
abstract class Animal{
    int num=33;
}
class Dog extends Animal{
    int num=100;
}
运行结果:
777

Process finished with exit code 0
  • 当你以后看到一个方法的返回值类型是一个接口 类型,你就返回一个该接口的子类对象
public class C3 {
    public static void main(String[] args) {
        Plusx cc=getPlusx();
       //Xx xx=(Xx) getPlusx();
        System.out.println(Plusx.num);
        System.out.println(cc.num);
    }
    public static Plusx getPlusx() {
    return new Xx();
    }
}
interface Plusx{
    int num=333;
}
class Xx implements Plusx {
    int num=777;
}
运行结果:
333
333

Process finished with exit code 0

链式编程

public class LianShi {
    public static void main(String[] args) {
        //正常风格
        Student stu=getStu();
        Student stu1 = stu.getStudent();
        int add=stu1.add(20,30);
        System.out.println(add);
        //链式编程的语法风格
        int add1=getStu().getStudent().add(770,330);
        System.out.println(add1);
        int add12=getStu().getStudent().show(3355889).getNum();
        System.out.println(add12);

    }
    public static  Student getStu(){
        Student student = new Student();
        student.num=999999999;
        return student ;
    }
}
class Student{
    public int getNum() {
        return num;
    }

    int num=20;
    public Student getStudent(){
        return new Student();
    }
    public Student getStudent1(){
        Student student = new Student();
        student.num=7777777;
        return student ;
    }
    public  int add(int a,int b){
        return a+b;
    }
    public  Student show(int num){
       this.num=num;
       return this;
    }
}
运行结果:
50
1100
3355889

Process finished with exit code 0

package

  • 概述:文件夹
  • 作用: 用来解决同一个路径下不能存在同名文件的问题(分类管理)
  • 包的划分:
    1. 按照功能
    2. 按照模块
  • 定义包的格式
    1. package 包名;
    2. 多级包用.分开即可
  • 定义包的注意事项
    1. package语句必须是程序的第一条可执行的代码
    2. package语句在一个java文件中只能有一个
    3. 如果没有package,默认表示无包名

import

  • 概述 不同包下的类之间的访问 java提供导包的功能
  • 格式
    import 包名; 这种方式导入是到类的名称 虽然可以最后写*,但是不建议。

四种权限修饰符

            本类	同一个包下(子类和无关类)  不同包下(子类) 不同包下(无关类)
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

      用的最多的就是:public
      
  • 修饰成员变量的关键字:

    1. 权限修饰符:private,默认的,protected,public

    2. 状态修饰符:static,final

      用的最多的就是:private
      
  • 修饰构造方法的关键字:

    1. 权限修饰符:private,默认的,protected,public

      用的最多的就是:public
      
  • 修饰成员方法的关键字:

    1. 权限修饰符:private,默认的,protected,public

    2. 状态修饰符:static,final

    3. 抽象修饰符:abstract

      用的最多的就是:public
      
  • :除此以外的组合规则:

    1. 成员变量:public static final
    2. 成员方法:public static ,public abstract ,public final

待续…

猜你喜欢

转载自blog.csdn.net/cx9977/article/details/107578640