类作为引用类型的参数和返回值时

1. 作为引用类型的参数和返回值时。

public class CatOPerator {
    //引用类型作为参数
    public void usecat(Cat c) {  //这里cat c相当与Cat c=new Cat ,就是创建了一个对象
        c.eat();
    }
    //引用类型Cat作为返回值
    public Cat getCat(){
        Cat cat = new Cat();
        return cat; //返回的是对象
    }
}

public class Demo {
    public static void main(String[] args) {
        CatOPerator c = new CatOPerator();    //创建操作类对象
        Cat ca = new Cat();            //创建猫类
        c.usecat(ca);

        Cat cat1 = c.getCat();
        cat1.eat();
    }
}

 

类作为参数时,它的变量可以调用方法,比如,Cat c作为参数。c用来调用方法。要在测试类中创建该类对象c.如:Cat c=new Cat();猫类对象c.在测试类调用方法,把类对象传入方法里c.usecat(ca);

类作为返回值时,返回的是该类的对象。

 

总结:类名作为方法形参,其实需要的是该类的对象

类名作为方法返回值,其实返回的是该类的对象

2. 抽象类名作为形参和返回值。

有抽象类:要采用多态的形式来创建对象。要有子类继承父类

  1. 总结:抽象类名作为形参和返回值。需要的是抽象类的子类对象和返回抽象类的子类对象
  2. 抽象类不能直接实例化,需要通过多态形式初始化

 

3.接口名作为形参和返回值

接口作为形参时,接口不能直接实例化,也需要多态形式创建对象。来调用方法。多态就需要子类来实现。

 

//接口作为形参
public interface Interface {
   void jump();           //接口里面用抽象方法


}

public class Animal {
   public void usejump(Interface a){       //Animal类中的方法要使用接口中的方法,就要把接口传进来,用接口对象来调用接口中的方法
       a.jump();
   }
}

public class Demo {
    public static void main(String[] args) {
        //创建animal对象来测试在Animal中使用接口中的方法好不好用/那么就要创建该对象,并调用该类的方法
        Animal animal = new Animal();
        Interface cat = new Cat();
        animal.usejump(cat);            //因为usejump方法中是接口类型参数,而接口又不能直接实例化,所以需要一个类来实现,多态形式初始化
                                        //有了实现类后,需要把实现类中的对象传入usejump()方法中
    }
}

public class Cat implements Interface{
    @Override
    public void jump() {
        System.out.println("猫可以跳高了");
    }
}

 

总结:接口作为形参,需要的是该接口的实现类对象

接口作为返回值,其实返回的是接口的实现类对象

 

5. 内部类

在类A中,在定义一个类BB就叫做内部类

内部类的访问特点:内部类可以访问外部类的成员,包括私有。

外部类要想访问内部类成员,必须要创建对象。

public class NeiClass {
    private int a = 20;

    public class Lei {      //类里面要有方法体
        public void use() {
            System.out.println(a);
        }
    }
    //      外部类方法调用内部类方法
    public void method() {          //想要使用内部类中的use方法,必须先创建内部类对象,在调用
        Lei lei = new Lei();    //创建内部类对象
        lei.use();               //调用内部类方法
    }
}

6. 成员内部类

根据内部类在类中的位置分为:

l 在类的成员位置:成员内部类

l 在类的局部位置:局部内部类(方法里面)

内部类和局部类外界类都不能直接访问。

 

内部类如何创建对象使用?

格式:外部类类名.内部类类名 对象名=new 外部类类名.内部类类名

这种格式不是私有的,如果是私有类就不行了

public class Deno {
    public static void main(String[] args) {
     NeiClass.chen  a=new NeiClass().new chen();
     a.methon1();

    }
}

内部类的方法如何调用

两种方式:

外部类类名.内部类类名 对象名=new 外部类类名.内部类类名 (非私有类)

l 在外部类中在定义一个方法,方法里创建内部类对象,再用对象名调用内部类方法(私有类)

 */
public class NeiClass {
    private int num = 10;

    private class chen {   //成员内部类  是私有的内部类,
        public void methon1() {
            System.out.println(num);
        }
    }

    public void method() {
        chen chen = new chen();
        chen.methon1();
    }


}

 

局部内部类:局部内部类是在方法中的类,外界无法直接使用,需要在方法内部创建对象并调用,该类可以访问外部类成员,也可以访问方法内局部变量。

 

7. 匿名内部类

匿名类前提:存在一个类或接口,这个类可以是具体类或抽象类

格式:

New 类名或者接口名(){

重写方法;

}

 

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

匿名内部类是一个局部内部类,需要在方法中写。

public class Outer  {
    public void method() {
     /*   new Inte(){               //匿名类格式,匿名类本质是一个子类匿名对象
            @Override              //方法重写
            public void show() {
                System.out.println("年后");
            }
        }.show();  */       //;别忘了写,然后用匿名对象调用里面的show方法。


         Inte r=new Inte() {               //匿名类格式,匿名类本质是一个子类匿名对象
            @Override              //方法重写
            public void show() {
                System.out.println("年后");
            }
        };
         r.show();
         r.show();
         r.show();//多次调用
    }
}

 

匿名内部类在开发中的使用?

public class Demo {
    public static void main(String[] args) {
        Jump ju = new Jump();
    /*   Jumpping cat = new Cat();
        ju.method(cat);*/


        ju.method(new Jumpping() {   //匿名类调用方法
            @Override
            public void jump() {
                System.out.println("猫可以跳高了");
            }
        });
        ju.method(new Jumpping() {
            @Override
            public void jump() {
                System.out.println("狗可以调高了");
            }
        });
    }
}

 

 

 

 

 

8. 常用API

Math:基本数字运算的方法。

没有构造方法,如果成员都是静态的,则可以通过类名直接调用

方法名

说明

Public static int abs(int a)

返回参数的绝对值

Ceil( double a)

返回一个大于或等于参数的最小值,等于一个整数

Floor( double a)

返回一个小于或等于参数的最小值,等于一个整数

Int round(float a)

四舍五入,返回最接近参数的int

Int max( int a,int b)

返回两个中最大值

Int min()

 

Pow(double a,double b)

返回ab次幂的值

Random()

返回值为double的正值

public class MathDemo{
    public static void main(String[] args) {
        System.out.println(Math.abs(88));
        System.out.println(Math.abs(-88));
        System.out.println("-----");
        System.out.println(Math.ceil(23.3)); //ceil返回一个大于或等于参数的最小整数(double类型)
        System.out.println("-----");
        System.out.println(Math.floor(23.3)); //floor返回一个小于于或等于参数的最小整数(double类型)
        System.out.println("-----");
        System.out.println(Math.round(77.6)); //round四舍五入,返回最接近参数的int 78
        System.out.println("-----");
        System.out.println(Math.max(23.3,45)); //返回两个中最大值
        System.out.println(Math.min(23,45)); //
        System.out.println("-----");
        System.out.println(Math.pow(2.5,2)); //返回a的b次幂的值   返回值double类型
        System.out.println("-----");
        System.out.println((int)(Math.random()*100)+1);         //返回double类型随机数,范围在[0.0,1.0).如何取整数
    }
}

 

9. System

常用方法,方法名

说明

Exit(int status)

终止当前JAVA虚拟机,非0表示异常终止

currentTimeMillis()

 

 

10. Object类的方法

l toString方法

l Equals

 
public class Demo {
    public static void main(String[] args) {
        Student s = new Student("林青霞",33);
        Student s1 = new Student("林青霞",33);
        //比较俩个对象内容是否相同
        System.out.println(s==s1); //这里比较的是两个对象的地址值,不同
        System.out.println(s.equals(s1));   //这里equals默认Object里的地址值,必须重写equals方法才能比较内容
       /* public boolean equals(Object obj) {
             this----s
             obj----s2
            return (this == obj);
        }*/


    }
}

 

11. 冒泡排序(必须会

如果n个数据排序,那么比较n-1

l 每次比较完,都会少一个数参与下次比较

public class MaoPao {
    public static void main(String[] args) {
        int[] arr={24,45,67,34,21,89,25};
        System.out.println("排序前"+method(arr));
        
        for(int x=0;x<arr.length-1;x++){
        for (int i = 0; i <arr.length-1-x ; i++) {
            if(arr[i]>arr[i+1]){
                int temp=arr[i];
                arr[i]=arr[i+1];
                arr[i+1]=temp;
            }
        }
        }
        System.out.println("排序后:"+method(arr));
    }

    public static String method(int[] arr){
        StringBuilder a=new StringBuilder();
        a.append("[");
        for (int i = 0; i < arr.length; i++) {
           if(i==arr.length-1){
               a.append(arr[i]);
           }else {
               a.append(arr[i]).append(",");
           }
        }
        a.append("]");
        String s = a.toString();
        return s;
    }
}

 

 

double a=120000;
double b=999.8;

BigDecimal a1 = new BigDecimal(String.valueOf(a));
BigDecimal b1 = new BigDecimal(String.valueOf(b));
BigDecimal c2 = a1.subtract(b1);
System.out.println(c2);

 

12. Arrays

public class Sort {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 5, 6, 78, 9};
        System.out.println("排序前:" + Arrays.toString(arr));
        Arrays.sort(arr);
        System.out.println("排序后:" +Arrays.toString(arr));

    }
}

排序前:[1, 2, 3, 5, 6, 78, 9]

排序后:[1, 2, 3, 5, 6, 9, 78]

 

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

        int[] arr = {1, 2, 3, 5, 6, 78, 9};
        Arrays.sort(arr);
        int i = Arrays.binarySearch(arr, -9);  //binarySearch获取指定键值的索引位置,如果数组中不存在,则返回实际在数组中的索引值的相反数,在-1.
                                                    //如果返回-1,表示该值不存在数组中
        System.out.println(i);


    }
}

 

posted @ 2020-02-03 17:52  Mrmukk  阅读(634)  评论(0编辑  收藏  举报