发布时间:2022-12-02 文章分类:编程知识 投稿人:王小丽 字号: 默认 | | 超大 打印

1、参数传递

1.1 类名作为形参和返回值

    方法的形参是类名,需要的是该类的对象;实际传递的是该对象的地址值

    方法的返回值是类名,返回的是该类的对象;实际传递的是该对象的地址值

public class Cat {
public void eat(){
System.out.println("猫吃鱼");
}
}
public class CatOperator {
//类名作为方法的形参
public void useCat(Cat c){ //Cat c = new Cat();
c.eat();
}
//类名作为方法的返回值
public Cat getCat(){
Cat c = new Cat();
return c;
}
}
public class CatDemo {
public static void main(String[] args) {
//创建操作类对象,并调用方法
CatOperator co = new CatOperator();
Cat c = new Cat();
co.useCat(c);
Cat c2 = co.getCat();
c2.eat();
}
}

1.2 抽象类作为形参和返回值(理解)

public class Cat extends Animal {
@Override
public void eat() {
System.out.println("猫吃鱼");
}
}
public abstract class Animal {
public abstract void eat();
}
public class AnimalOperator {
public void userAnimal(Animal a){
a.eat();
}
public Animal getAnimal(){
Animal a = new Cat();
return a;
}
}
public class AnimalDemo {
public static void main(String[] args) {
AnimalOperator ao = new AnimalOperator();
Animal a = new Cat();
ao.userAnimal(a);
Animal ao2 = ao.getAnimal();
ao2.eat();
}
}

1.3 接口作为形参和返回值(理解)

public interface Jumpping {
void jump();
}
public class Cat implements Jumpping{
@Override
public void jump() {
System.out.println("猫可以跳高");
}
}
public class JumppingOperator {
public void useJumpping(Jumpping j){//Jumpping j = new Cat();
j.jump();
}
public Jumpping getJumpping(){
Jumpping j = new Cat();
return j;
}
}
public class JumppingDemo {
public static void main(String[] args) {
JumppingOperator jo = new JumppingOperator();
Jumpping j = new Cat();
jo.useJumpping(j);
Jumpping j2 = jo.getJumpping();
j2.jump();
}
}

2、内部类

2.1 内部类的基本使用(理解)

class 外部类名{
修饰符 class 内部类名{
}
}
public class Outer {
public class Inner{
}
}
public class Outer {
private int num = 10;
public class Inner{
public void show(){
System.out.println(num);
}
}
public void method(){
Inner i = new Inner();
i.show();
}
}

2.2 成员内部类(理解)

public class Outer {
private int num =10;
private class Inner {
public void show(){
System.out.println(num);
}
}
public void method(){
Inner i = new Inner();
i.show();
}
}
public class OuterDemo {
public static void main(String[] args) {
Outer o = new Outer();
o.method();
}
}

2.3 局部内部类(理解)

public class Outer {
private int num = 10;
public void method(){
int num2 = 20;
class Inner{
public void show(){
System.out.println(num);
System.out.println(num2);
}
}
Inner i = new Inner();
i.show();
}
}
public class OuterDemo {
public static void main(String[] args) {
Outer o = new Outer();
o.method();
}
}

2.4 匿名内部类(应用)

new Inter(){
@Override
public void method(){}
}
Inter i = new Inter(){
@Override
public void method(){
}
}
public interface Inner {
public void method();
}
public class InnerDemo {
public static void main(String[] args) {
new Inner(){
@Override
public void method(){
System.out.println("我是匿名内部类");
}
}.method();
}
}

2.4 匿名内部类在开发中的使用(应用)

public interface Jumpping {
void jump();
}
public class Cat implements Jumpping{
@Override
public void jump() {
System.out.println("猫可以跳高了");
}
}
public class Dog implements Jumpping{
@Override
public void jump() {
System.out.println("狗可以跳高了");
}
}
public class JumppingOperator {
public void method(Jumpping j){
j.jump();
}
}
public class JumppingDemo {
public static void main(String[] args) {
//创建接口操作类对象调用method方法
JumppingOperator jo = new JumppingOperator();
//方式一:传统方式
//操作猫的行为
Jumpping j1 = new Cat();
jo.method(j1);
//操作狗的行为
Jumpping j2= new Dog();
jo.method(j2);
//方式二:匿名内部类的方式
//操作猫的行为
jo.method(new Jumpping() {
@Override
public void jump() {
System.out.println("猫可以跳高了");
}
});
//操作狗的行为
jo.method(new Jumpping() {
@Override
public void jump() {
System.out.println("狗可以跳高了");
}
});
}
}

3、常用API

3.1 Math(应用)

day12_内部类&API

3.2 System(应用)

day12_内部类&API

public class SystemDemo {
public static void main(String[] args) {
//需求:在控制台输出1-10000,计算这段代码执行了多少毫秒
//获取开始的时间节点
long start = System.currentTimeMillis();
for (int i = 1; i <= 10000; i++) {
System.out.println(i);
}
long end = System.currentTimeMillis();
System.out.println("这段代码执行耗时:"+(end-start)+"ms");
}
}

3.3 Object类的toString方法(应用)

public class Student extends Object{
private String name;
private int age;
public Student(){
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class ObjectDemo {
public static void main(String[] args) {
Student s  = new Student();
s.setName("林青霞");
s.setAge(30);
System.out.println(s);
System.out.println(s.toString());
}
}
Student{name='林青霞', age=30}
Student{name='林青霞', age=30}

3.4 Object类的equals方法(应用)

public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Student)) return false;
Student student = (Student) o;
if (getAge() != student.getAge()) return false;
return getName() != null ? getName().equals(student.getName()) : student.getName() == null;
}
@Override
public int hashCode() {
int result = getName() != null ? getName().hashCode() : 0;
result = 31 * result + getAge();
return result;
}
}
public class ObjectDemo {
public static void main(String[] args) {
Student s1 = new Student();
s1.setName("林青霞");
s1.setAge(30);
Student s2 = new Student();
s2.setName("林青霞");
s2.setAge(30);
//需求:比较两个对象的内容是否相同
System.out.println(s1.equals(s2));
}
}

3.5 冒泡排序原理(理解)

day12_内部类&API

3.6 冒泡排序的实现(理解)

public class ArrayDemo {
public static void main(String[] args) {
//定义一个数组
int[] arr = {24, 69, 80, 57, 13};
System.out.println("排序前:"+ArrayDemo.arrayToString(arr));
//排序的轮数
for (int i = 0; i < arr.length-1; i++) {
//每轮排序次数
for (int j = 0; j < arr.length-1-i; j++) {
if (arr[j]>arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
System.out.println("排序后:"+ArrayDemo.arrayToString(arr));
}
public static String arrayToString(int[] arr){
StringBuilder sb = new StringBuilder();
sb.append("[");
for (int i = 0; i < arr.length; i++) {
if (i == arr.length-1){
sb.append(arr[i]);
}else {
sb.append(arr[i]).append(",");
}
}
sb.append("]");
return sb.toString();
}
}

3.7 Array(应用)

day12_内部类&API

    1、构造方法用private修饰

    2、成员用public static修饰

public class ArrayDemo {
public static void main(String[] args) {
//定义一个数组
int[] arr = {26,69,80,57,13};
System.out.println("排序前:"+ Arrays.toString(arr));
//使用sort方法进行排序
Arrays.sort(arr);
System.out.println("排序后:"+ Arrays.toString(arr));
}
}