Java面向对象编程(基础部分)

面向对象编程(基础部分)

类与对象

在这里插入图片描述

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8VSzkDnh-1634262458719)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210911094845026.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TRv5bHMB-1634262458720)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210911094901314.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yS2XckJ1-1634262458722)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210911094917008.png)]

01:

public class ObjectWorkDemo
{
    public static void main(String[] args)
    {
        Cat cat1 = new Cat();
        cat1.name = "Tom";
        cat1.age = 3;
        cat1.color = "white";
        
        Cat cat2 = new Cat();
        cat2.name = "xiaohua";
        cat2.age = 100;
        cat2.color = "flower";
    }
}


class Cat
{
    String name;
    int age;
    String color;
}

在这里插入图片描述

对象内存布局

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-43cd5Uxz-1634262458724)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210911100536711.png)]

属性/成员变量

在这里插入图片描述

注意事项和细节说明

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-n7GpEl0U-1634262458726)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210911101007798.png)]

01:

public class PropertiesDetail
{
    public static void main(String[] args)
    {
        Person p1 = new Person();//p1 对象引用   Person() 才是真正的数据空间(真正的对象)
    }
}

class Person
{
    int age;
    String name;
    double sal;
    boolean isPass;
}

创建对象

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LHUJGRLp-1634262458727)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210911102620089.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3Rx49qAA-1634262458728)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210911104714065.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VVDaCpDs-1634262458730)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210911105309738.png)]

创建过程

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lZWEznWs-1634262458730)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210911105556251.png)]

小练习

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nvMaLcKW-1634262458731)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210911105617934.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-L45ol96y-1634262458731)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210911124116862.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lLhk78PE-1634262458732)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210911124127654.png)]

成员方法

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-B6njAQSR-1634262458734)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210911124344815.png)]

public class PersonWorkDemo
{
    public static void main(String [] args)
    {
     	Person p1 = new Person();
        p1.speak();
        p1.cal01();
        p1.cal02(5);
        int res = p1.getSum(10,5);
        System.out.println(res);
    }
}


 class Person
    {
        String name;
        int age;
        
        public void speak()
        {
            System.out.println("I am good person");
        }
        
     public void cal01()
     {
         int res = 0;
         for (int i = 1;i<=1000;i++)
         {
             res+=i;
         }
         System.out.println(res);
     }
     
     public void cal02(int n)
     {
         int res = 0;
         for (int i = 1;i<=n;i++)
         {
             res+=i;
         }
         System.out.println(res);
     }
     
     public int getSum(int a,int b)
     {
         return a+b;
     }
        
    }

方法调用机制

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bfx75hdM-1634262458735)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210911131525063.png)]

成员方法的好处

  • 提高代码的复用性

  • 可以将实现的细节封装起来,然后供其他用户来调用即可

成员方法的定义

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rQvu9vcY-1634262458736)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210911132524518.png)]

注意事项和使用细节

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Derbz0oT-1634262458737)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210911133629896.png)]

在这里插入图片描述

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Cysa5wRn-1634262458739)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210912094923766.png)]

小练习

在这里插入图片描述

01:

public class TestWork
{
    public static void main(String [] args)
    {
        AA a = new AA();
        boolean b = a.isOdd(1);
        if (b) System.out.println("yes");
        else System.out.println("no");
    }
}


class AA
{
    public boolean isOdd(int num)
    {
        if num%2!=0?true:false;
    }
    
    
    public void print(int row,int col,char c)
    {
        for (int i = 0;i<row;i++)
        {
            for (int j = 0;j<col;j++)
            {
                System.out.print(c);
            }
            System.out.println();
        }
	}
}

成员方法传参机制

  • 基本数据类型,传递的是值(值拷贝),形参的任何改变不影响实参

01:

public class MethodParameter01
{
    public static void main(String[] args)
    {
        int a = 10;
        int b = 20;
        AA object = new AA();
        object.swap(a,b);
        
        
        System.out.println("a = "+a+" b = "+b);//a = 10 b = 20
    }
}


class AA
{
    public void swap(int a,int b)
    {
        System.out.println("a = "+a+" b = "+b);//a = 10 b = 20
        int tmp = a;
        a = b;
        b = tmp;
        System.out.println("Later a = "+a+" b = "+b);//a = 20 b = 10
    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ru7vLV8C-1634262458741)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210912102927869.png)]

  • 引用类型传递的是地址(传递也是值,但是值是地址),可以通过形参影响实参

02:

public class MethodParameter02
{
    public static void main(String[] args)
    {
     	B b = new B(); 
        int [] arr = {1,2,3};
        b.test100(arr);
        System.out.println("main:");
        for (int i = 0;i<arr.length;i++)
        {
            System.out.print(arr[i]+"\t");//200 2 3
		}
        System.out.println();
        
        
        Person p = new Person();
        p.name = "Tom";
        p.age = 10;
        
        b.test200(p);
        System.out.println("main age =  "+p.age);//1000
    }
}


class Person
{
    String name;
    int age;
}


class B
{
    
    public void test200(Person p)
    {
        p.age = 1000;
	}
    
    public void test100(int [] arr)
    {
        arr[0] = 200;
        for (int i = 0;i<arr.length;i++)
        {
            System.out.print(arr[i]+"\t");//200 2 3
		}
        System.out.println();
	}
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EAYzefYc-1634262458741)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210912103258339.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-z5fUpeCI-1634262458742)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210912103548636.png)]

这里的"tom"p会被当做垃圾销毁掉

小练习

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xgV0tBhN-1634262458743)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210912104410211.png)]

public class TestDemo
{
    public static void main(String[] args)
    {
        Person p = new Person();
        p.name = "Jack";
        p.age = 30;
        
        MyTools myT = new MyTools();
        Person p2 = myT.copyPerson(p);
        
        System.out.println(p==p2);//false
        
    }
}


class Person
{
    String name;
    int age;
}


class MyTools
{
    public Person copyPerson (Person p)
    {
        Person p2 = new Person();
        p2.name = p.name;
        p2.age = p.age;
        return p2;
	}
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oJc7IwCE-1634262458744)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210912104843750.png)]

如果此时修改p2.name,并不会改变p.name,p2.name会指向一个新地址

方法递归调用

递归举例

01:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4VDCDYuE-1634262458745)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210912120310716.png)]

public class Recursion01
{
    public static void main(String [] args)
    {
        T t1 = new T();
        t1.test(4);
    }
}

class T
{
    public void test(int n)
    {
        if (n>2)
        {
            test(n-1);
        }
        System.out.println("n = "+n);
    }
}

//n = 2
//n = 3
//n = 4

02:

public class Recursion01
{
    public static void main(String [] args)
    {
        T t1 = new T();
        t1.test(4);
    }
}

class T
{
    public void test(int n)
    {
        if (n>2)
        {
            test(n-1);
        }
        else
        {
      	  System.out.println("n = "+n);
        }
    }
}

//n = 2

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gxOPqETA-1634262458746)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210912121211032.png)]

03:

public class Recursion01
{
    public static void main(String [] args)
    {
        T t1 = new T();
        int res = t1.factorial(5);
        System.out.println(res);//120
    }
}

class T
{
    public int factorial(int n)
    {
        if (n==1)
        {
            return 1;
		}
        else
        {
            return factorial(n-1)*n;
		}
	}
}

递归重要规则

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-93JFCXLU-1634262458746)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210912121223161.png)]

小练习

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Vuq8Fu9o-1634262458747)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210912122533579.png)]

01:

class T
{
    public int fab(int n)
    {
        if (n>=1){
        if (n==1 || n==2)
        {
            return 1;
        }
        else
        {
            return fab(n-1)+fab(n-2);
        }
     	}
        else
        {
            System.out.println("Input Error");
            return -1;
		}
	}
}

02:

class T
{
    public int peach(int day)
    {
        if (day==10)
        {
            return 1;
        }
        else if (day >= 1 && day <= 9)
        {
            return (peach(day+1)+1)*2;
        }
        else
        {
            System.out.println("day在1-10");
            return -1;
        }
    }
}

老鼠出迷宫

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yhbuL2yW-1634262458748)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210912123810795.png)]

03:

!

汉诺塔

八皇后

略!

方法重载

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XzHgfvfk-1634262458749)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210912124444384.png)]

案例

01:

class MyCalculator
{
    public int calculate(int n1,int n2)
    {
        return n1+n2;
    }
    
    public double calculate(int n1,double n2)
    {
        return n1+n2;
    }
    
    public double calculate(double n2,int n1)
    {
        return n1+n2;
    }
    
    public int calculate(double n1,int n2,int n3)
    {
        return n1+n2+n3;
    }
}

注意事项和使用细节

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WqBx20DY-1634262458750)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210912125600960.png)]

小练习

01:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-R6hIDLbX-1634262458751)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210912130412121.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-aNREXO2s-1634262458752)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210912130512242.png)]

01:

public class Test
{
    public static void main(String[] args)
    {
        Methods obj = new Methods();
        obj.m(8);
        obj.m(8,6);
        obj.m("dadas");
    }
}



class Methods
{
    public void m(int n)
    {
        System.out.println(n*n);
    }
    
    public void m(int n1,int n2)
    {
        System.out.println(n1*n2);
	}
    
    public void m(String str)
    {
        System.out.println(str);
	}
}

02:

public class Test
{
    public static void main(String[] args)
    {
        Methods obj = new Methods();
       
    }
}



class Methods
{
   public int findMax(int n1,int n2)
   {
       return n1>n2?n1:n2;
   }
    
    public double findMax(double n1,double n2)
    {
        return n1>n2?n1:n2;
    }
    
    public double findMax(double n1,double n2,double n3)
    {
        double max1 = n1>n2?n1:n2;
        double  max2 = max1>n3?max1:n3;
        return max2;
	}
}

可变参数

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vkTSr0wO-1634262458754)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210912132031070.png)]

案例

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5loUNpNS-1634262458755)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210912132046383.png)]

01:

public VariableParameter01
{
    public static void main(String[] args)
    {
        HspMethod obj = new HspMethod;
        int res = obj.sum(1,5,100);
        System.out.println(res);//106
    }
}


class HspMethod
{
    public int sum(int... nums)
    {
        int sum =0 ;
        for (int i = 0;i<nums.length;i++)
        {
            sum+=nums[i];
        }
        return sum;
	}
}

注意事项和使用细节

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5wXf9jxM-1634262458757)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210912133042199.png)]

  • 可变参数的实参可以为数组

01:

public class VariableParameterDetail
{
    public static void(String[] args)
    {
        int[] arr = {1,2,3};
        T t1 = new T();
        t1.f1(arr);
    }
}


class T
{
    public void f1(int... nums)
    {
        System.out.println(nums.length);//3
    }
}
  • 可变参数可以和普通类型的参数一起放在形参列表,但必须保证可变参数在最后

01:

class T
{
    public void f2(double str,double...nums)
    {
        
    }
}
  • 一个形参列表中只能出现一个可变参数
public int sum(String str,int... nums01,String... nums02);//Error

小练习

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-B5wUiUiR-1634262458758)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210912134030009.png)]

01:

public class HspMethod
{
    public String showScore(String name,double... scores)
    {
        double totalScore = 0;
        for (int i = 0;i<scores.length;i++)
        {
            totalScore+=scores[i];
        }
        return name+scores.length+totalScore;
    }
}

作用域

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3c782Eoj-1634262458759)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210912183942814.png)]

注意事项和使用细节

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KIPfyoMt-1634262458760)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210912184718970.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7oPSckdA-1634262458761)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210912185318046.png)]

  • 属性和局部变量可以重名,访问时遵循就近原则

01:

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


class Person
{
 	String name = "Tom";
    
    public void say()
    {
        String name = "king";
        System.out.println("say() name = "+name);//say() name = king
    }
}

02:

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


class Person
{
 	String name = "Tom";
    
    public void say()
    {
      //  String name = "king";
        System.out.println("say() name = "+name);//say() name = Tom
    }
}
  • 在同一个作用域中,比如在同一个成员方法中,两个局部变量,不能重名

01:

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

class Person
{
    String address = "beijin";
    //String address = "guanzhou";//Error  重复定义
    String name = "hsp";
    
}

01:

public class ScopeDemo
{
    public static void main(String[] args)
    {
        Person p1 = new Person();
        
        p1.say();//当执行say()方法时,say方法的局部变量比如name会创建,当say执行完毕后,name局部变量销毁,但是属性(全局变量)仍然可以使用
    }
}


class Person
{
 	String name = "Tom";
    
    public void say()
    {
        String name = "king";
        System.out.println("say() name = "+name);
    }
}
  • 作用域范围不同
class T
{
    public void test()
    {
        Person p1 = new Person();
        System.out.println(p1.name);
    }
    
    public void test02(Person p)
    {
        System.out.println(p.name);
    }
}


class Person
{
    String name = "jack";
}

构造方法/构造器

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pCp45be4-1634262458763)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210912190522655.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PXKufofQ-1634262458763)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210912190533136.png)]

  • 案例

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eVgXujpm-1634262458764)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210912190541940.png)]

01:

public class Constructor01
{
    public static void main(String[] args)
    {
        Person p1 = new Person("jack",80);
        System.out.println(p1.name+" "+p1.age);
    }
}

class Person
{
    String name;
    int age;
    
    public Person(String pName,int pAge)
    {
        name = pName;
        age = pAge;
    }
}

注意事项和使用细节

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Liscr5sM-1634262458765)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210912191154597.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MHIhrN2L-1634262458765)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210912191503042.png)]

小练习

01:

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

class Person
{
    String name;
    int age;
    
    public Person()
    {
        age = 18;
    }
    
    public Person(String pName,int pAge)
    {
        name = pName;
        age = pAge;
    }
}

对象创建的流程分析

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7xGKAGRy-1634262458766)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210912195911820.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VCW5L80W-1634262458767)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210912195859358.png)]

javap

  • P243

this关键字

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5psceFrD-1634262458767)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210912201449121.png)]

01:

public class This01
{
    public static void main(String[] args)
    {
        Dog dog1 = new Dog("大壮",3);
        dog1.info();
	}
}

class Dog
{
    String name;
    int age;
  //  public Dog(String dName,int dAge)
   // {
   //     name = dName;
   //     age = dAge;
   // }
    
    public Dog(String name,int age)
    {
        this.name = name;
        this.age = age;
    }
    
    public void info()
    {
        System.out.println(name+" "+age+" ");
    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gt7em4Dc-1634262458768)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210912203016615.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GqJoCTaz-1634262458768)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210912203029101.png)]

public class This01
{
    public static void main(String[] args)
    {
        Dog dog1 = new Dog("大壮",3);
        System.out.println("dog1的hashcode = "+dog1.hashCode());
        dog1.info();
         Dog dog2 = new Dog("小东",7);
        System.out.println("dog1的hashcode = "+dog2.hashCode());
        dog2.info();
    }
}


class Dog
{
    String name;
    int age;
  //  public Dog(String dName,int dAge)
   // {
   //     name = dName;
   //     age = dAge;
   // }
    
    public Dog(String name,int age)
    {
        this.name = name;
        this.age = age;
        
        System.out.println("this.hashCode = "+this.hashCode());
    }
    
    public void info()
    {
        System.out.println(name+" "+age+" ");
    }
}

this使用细节

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JgK5ZZ4y-1634262458769)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210912224623249.png)]

01:

class T
{
    public void f1()
    {
        System.out.println("f1ok");
    }
    
    public void f2()
    {
        System.out.println("f2ok");
        f1();
        this.f1();
    }
}

02:

class T
{
    public T()
    {
        this("jack",100);
        System.out.println("T() 构造器");
        //在这里访问T(String name,int age)
        //this("jack",100);//ERROR 注意:如果有this(参数列表);必须放在第一条语句
	}
    
    public T(String name,int age)
    {
        System.out.println("T(String name,int age)构造器");
    }
    
    
}

案例

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rdg0Mc9x-1634262458770)(C:\Users\Tom\AppData\Roaming\Typora\typora-user-images\image-20210912225252125.png)]

01:

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


class Person
{
    String name;
    int age;
    
    public Person(String name,int age)
    {
        this.name = name;
        this.age = age;
    }
    
    public boolean compareTo(Person p)
    {
       return this.name.equals(p.name) && this.age==p.age;
    }
}






);
        System.out.println("dog1的hashcode = "+dog2.hashCode());
        dog2.info();
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值