Java面向对象编程(中级)

面向对象编程(中级)

访问修饰符

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

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

封装

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

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

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

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

01:

public class Encapsulation01
{public static void main(String[] args){Person person = new Person();person.name = "Tom";person.setAge(30);person.setSalary(30000);}
}class Person
{public String name;private int age;private double salary;public void setSalary(double salary){this.salary = salary;}public double getSalary(){return this.salary;}public void setAge(int age){if (age >= 1 && age <= 123)this.age = age;else {System.out.println("Input Error");this.age = 18;//给个默认age}}public int getAge(){return this.age;}public void setName(String name){if (name.length() >= 2 && name.length() <= 6)this.name = name;else{System.out.println("Input Error");this.name = "wuming";}}public String getName(){return this.name;}public String printElem(){return "name = "+name+" age = "+age+" salary = "+salary;}}

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

01:

public class Encapsulation01
{public static void main(String[] args){Person person = new Person();person.name = "Tom";person.setAge(30);person.setSalary(30000);}
}class Person
{public String name;private int age;private double salary;public Person(){}public Person(String name,int age,double salary){//this.name = name;// this.age = age;// this.salary = salary;setName(name);setAge(age);setSalary(salary);}public void setSalary(double salary){this.salary = salary;}public double getSalary(){return this.salary;}public void setAge(int age){if (age >= 1 && age <= 123)this.age = name;else {System.out.println("Input Error");this.age = 18;//给个默认age}}public int getAge(){return this.age;}public void setName(String name){if (name.length() >= 2 && name.length() <= 6)this.name = name;else{System.out.println("Input Error");this.name = "wuming";}}public String getName(){return this.name;}public String printElem(){return "name = "+name+" age = "+age+" salary = "+salary;}}

小练习

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

package HelloDemo;public class Account {private String name;private double balance;private String password;public Account(){}public Account(String name, double balance, String password) {this.setBalance(balance);this.setName(name);this.setPassword(password);}public String getName() {return name;}public void setName(String name) {if(name.length() >= 2 && name.length() <= 4)this.name = name;else{System.out.println("Input Error");this.name = "wuming";}}public double getBalance() {return balance;}public void setBalance(double balance) {if (balance > 20)this.balance = balance;else {System.out.println("Input Error,balance = 0");}}public String getPassword() {return password;}public void setPassword(String password) {if (password.length()==6)this.password = password;else{System.out.println("Input Error password = 000000");this.password = "000000";}}public void showInfo(){//可以增加权限的校验System.out.println("name = "+name+" balance = "+balance+" password = "+password);}
}//-------------------------------------------------------------------------------package HelloDemo;public class TestAccount {public static void main(String[] args){Account account = new Account();account.setName("Tom");account.setBalance(60);account.setPassword("123456");account.showInfo();}
}

继承

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

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

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

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

01:

package JiCWorkDemo;public class Pupil {public String name;public int age;private double score;public void setScore(double score) {this.score = score;}public void testing(){System.out.println("pupil name= "+name);}public void showInfo(){System.out.println("name = "+name+" age = "+age+" score = "+score);}
}//===========================================package JiCWorkDemo;public class Graduate extends Pupil{public void testing(){System.out.println("Graduate name= "+name);}
}//===========================================package JiCWorkDemo;public class WorkDemo {public static void main(String[] args){Pupil pupil = new Pupil();pupil.name = "Tom";pupil.age = 11;pupil.testing();pupil.setScore(50);pupil.showInfo();System.out.println("========");Graduate graduate = new Graduate();graduate.name = "Jack";graduate.age = 23;graduate.testing();graduate.setScore(80);graduate.showInfo();}
}

继承的深入讨论/细节问题

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

01:

package JiCWorkDemo;public class Base {public int n1 = 100;protected  int n2 = 200;int n3 = 300;private int n4 = 400;public int getN4(){return n4;}public void callTest400(){test400();}public Base(){System.out.println("Base()......");}public void test100(){System.out.println("test100");}protected void test200(){System.out.println("test200");}void test300(){System.out.println("test300");}private void test400(){System.out.println("test400");}}//==============================================package JiCWorkDemo;public class Son extends Base{public Son(){System.out.println("sub()...");}public void say0k(){System.out.println(n1+n2+n3);test100();test200();test300();//System.out.println(n4);//Error//test400();//ErrorSystem.out.println("n4 = "+getN4());callTest400();}
}

在这里插入图片描述

在这里插入图片描述

**加粗样式**

在这里插入图片描述

继承的本质分析

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

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

如果查找过程中,父类的age是private,而爷爷类的age是public,但还是会报错,因为找到父类的age是private以后就会报错,不会去找爷爷类的age

小练习

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

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

结果如下:

我是A类

hahah我是B类的有参构造

我是c类的有参构造

我是c类的无参构造

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

01:

package ExerciseDemoWork;public class Computer {private String cpu;private int memory;private int disk;public Computer(String cpu, int memory, int disk) {this.cpu = cpu;this.memory = memory;this.disk = disk;}public String getDetails(){return "cpu = "+cpu+" memory = "+memory+" disk = "+disk;}public String getCpu() {return cpu;}public void setCpu(String cpu) {this.cpu = cpu;}public int getMemory() {return memory;}public void setMemory(int memory) {this.memory = memory;}public int getDisk() {return disk;}public void setDisk(int disk) {this.disk = disk;}}//============================================================================package ExerciseDemoWork;public class PC extends Computer{private String brand;public PC(String cpu, int memory, int disk, String brand) {super(cpu, memory, disk);this.brand = brand;}public String getBrand() {return brand;}public void setBrand(String brand) {this.brand = brand;}public void showInfo(){System.out.println("Pc  = ");System.out.println(getDetails()+"brand = "+brand);}
}//===========================================================================package ExerciseDemoWork;public class NotePad extends Computer{private String color;public NotePad(String cpu, int memory, int disk, String color) {super(cpu, memory, disk);this.color = color;}
}//=============================================================================package ExerciseDemoWork;public class Test {public static void main(String[] args){PC pc = new PC("intel", 16, 500, "IBM");pc.showInfo();}
}

super关键字

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

01:

package SuperWorkDemo;public class B extends A{public void hi() {System.out.println(super.n1 + " " +super.n2 + " " + super.n3);//System.out.println(super.n4);//Error}public void ok(){super.test100();super.test200();super.test300();//super.test400();//Error}public B(){super("jack");}//        public void hello()
//        {
//            //super();//error
//        }}//==========================================================================package SuperWorkDemo;public class A {public int n1 = 100;protected int n2 = 200;int n3 = 300;private int n4 = 400;public void test100(){};protected void test200(){};void test300(){};private void test400(){};public A(){}public A(String str){}
}

细节

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

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

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

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

方法重写/覆盖

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

01:

package OverrideWorkDemo;public class Animal {public void cry(){System.out.println("动物叫唤");}}//=============================================================package OverrideWorkDemo;public class Dog extends Animal{public void cry(){System.out.println("Small dog crying");}
}//==================================================================package OverrideWorkDemo;public class Override01 {public static void main(String[] args){Dog dog = new Dog();dog.cry();}
}

注意事项和使用细节

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

小练习

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

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

01:

package PersonWorkDemo;public class Person {private String name;private int age;public String say(){return "name = "+name+" age = "+age;}public Person(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;}}//====================================package PersonWorkDemo;public class Student extends Person{private int id;private double score;public Student(String name, int age, int id, double score) {super(name, age);this.id = id;this.score = score;}public String say(){return super.say()+" id = "+id+" score = "+score;}public int getId() {return id;}public void setId(int id) {this.id = id;}public double getScore() {return score;}public void setScore(double score) {this.score = score;}
}//=================================================package PersonWorkDemo;public class OverrideExercise {public static void main(String[] args){Person jack = new Person("jack", 10);System.out.println(jack.say());Student smith = new Student("smith",20,123456,99.8);System.out.println(smith.say());}//    name = jack age = 10
//    name = smith age = 20 id = 123456 score = 99.8
}

多态

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

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

在这里插入图片描述

01:

package Animal;public class PolyObject {public static void main(String[] args){Animal animal = new Dog();animal.cry();animal = new Cat();animal.cry();}
}//=====================================================================package Animal;public class Dog extends Animal{@Overridepublic void cry() {System.out.println("Dog cry cry......");}
}//===================================================================package Animal;public class Cat extends Animal{@Overridepublic void cry() {System.out.println("Cat cry cry....." );}}//=================================================================package Animal;public class Animal {public void cry(){System.out.println("Animal cry...");}}

多态注意事项和细节讨论

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

在这里插入图片描述

01:

public class PolyDetail
{public static void main(String[] args){Animal animal = new Cat();Object obj = new Cat();System.out.println("ok~~");}
}
  • 可以调用父类中的所有成员(需遵守访问权限),但是不能调用子类的特有的成员,因为在编译阶段,能调用哪些成员,是由编译类型来决定的

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

在这里插入图片描述

多态的向下转型

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

01:

Animal animal = new Cat();Cat cat = (Cat)animal;
cat.catchMouse();

02:

Animal animal = new Cat();Dog dog = (Dog)animal;//Error

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

01:

package PolyDetailDemo;public class PolyDetailedDemo {public static void main(String[] args){Base base =     new Sub();System.out.println(base.count);//10Sub sub = new Sub();System.out.println(sub.count);//20}}class Base
{int count = 10;
}class Sub extends Base{int count = 20;
}

02:

public class PolyDetail03
{public static void main(String[] args){BB bb = new BB();System.out.println(bb instanceof BB);//trueSystem.out.println(bb instanceof AA);//trueAA aa = new BB();System.out.println(aa instanceof AA);//trueSystem.out.println(aa instanceof BB);//trueObject obj = new Object();System.out.println(obj instanceof AA);//falseString str = "hello";System.out.println(str instanceof Object);//true}
}class AA{}class BB extends AA{}

小练习

01:

public class PolyExercise01
{public static void main(String[] args){double d = 13.4;//oklong l = (long)d;//okSystem.out.println(l);//13int in = 5;//okboolean b = (boolean)in;//error int -> booleanObject obj = "Hello";//ok 向上转型String objStr = (String)obj;//ok 向下转型System.out.println(objStr);//helloObject objPri = new Integer(5);//ok 向上转型String str = (String)objPri;//Error 指向Integer的父类引用,转成StringInteger str1 = (Integer)objPri;//ok 向下转型}
}

02:

package PolyDemoWork;public class Base {int cnt = 10;public void display(){System.out.println(this.cnt);}
}class Sub extends Base{int cnt = 20;public void display(){System.out.println(this.cnt);}}//==============================================================================package PolyDemoWork;public class PolyDemo {public static void main(String[] args){Sub s = new Sub();System.out.println(s.cnt);//20s.display();//20Base b = s;System.out.println(b==s);//trueSystem.out.println(b.cnt);//10b.display();//20}
}

java的动态绑定机制

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

01:

package PolyDemoWork02;public class A {public int i = 10;public int sum(){return get()+10;}public int sum1(){return i+10;}public int get(){return i;}}//====================================================================================package PolyDemoWork02;public class B extends A{public int i = 20;public int sum(){return i+20;}public int get(){return i;}public int sum1(){return i+10;}
}//=========================================================================================package PolyDemoWork02;public class PolyMainDemo {public static void main(String[] args) {A a = new B();System.out.println(a.sum());//40System.out.println(a.sum1());//30}
}

02:

package PolyDemoWork02;public class B extends A{public int i = 20;
//    public int sum()
//    {
//        return i+20;
//    }public int get(){return i;}public int sum1(){return i+10;}
}//====================================================================package PolyDemoWork02;public class A {public int i = 10;public int sum(){return get()+10;}public int sum1(){return i+10;}public int get(){return i;}}//==================================================================================package PolyDemoWork02;public class PolyMainDemo {public static void main(String[] args) {A a = new B();System.out.println(a.sum());//30System.out.println(a.sum1());//30}
}

03:

package PolyDemoWork02;public class B extends A{public int i = 20;
//    public int sum()
//    {
//        return i+20;
//    }public int get(){return i;}//    public int sum1()
//    {
//        return i+10;
//    }
}//====================================================================================package PolyDemoWork02;public class A {public int i = 10;public int sum(){return get()+10;}public int sum1(){return i+10;}public int get(){return i;}}//==============================================================================package PolyDemoWork02;public class PolyMainDemo {public static void main(String[] args) {A a = new B();System.out.println(a.sum());//30System.out.println(a.sum1());//20}
}

多态的应用

多态数组

在这里插入图片描述

01:

package Person;public class Person {private String name;private int age;public Person(String name, int age) {this.name = name;this.age = age;}public String say(){return name+" "+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;}
}//=========================================================package Person;public class Student extends Person{private double score;public Student(String name, int age, double socre) {super(name, age);this.score = socre;}public double getSocre() {return score;}public void setSocre(double socre) {this.score = socre;}@Overridepublic String say() {return super.say()+" score = "+score;}
}//==========================================================================package Person;public class Teacher extends Person{private double sal;public Teacher(String name, int age, double sal) {super(name, age);this.sal = sal;}public double getSal() {return sal;}public void setSal(double sal) {this.sal = sal;}@Overridepublic String say() {return super.say()+" sal = "+sal;}
}//==========================================================================package Person;public class PolyArray {public static void main(String[] args){Person[] persons = new Person[3];persons[0] = new Person("Tom",20);persons[1] = new Student("Jack",30,100);persons[2] = new Teacher("Bob",33,4500);for (int i = 0;i<persons.length;i++){persons[i].say();}}
}

02:

package Person;public class Person {private String name;private int age;public Person(String name, int age) {this.name = name;this.age = age;}public String say(){return name+" "+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;}
}//=================================================================================package Person;public class Student extends Person{private double score;public Student(String name, int age, double socre) {super(name, age);this.score = socre;}public double getSocre() {return score;}public void setSocre(double socre) {this.score = socre;}@Overridepublic String say() {return super.say()+" score = "+score;}public void study(){System.out.println("student = "+getName());}
}//=========================================================================package Person;public class Teacher extends Person{private double sal;public Teacher(String name, int age, double sal) {super(name, age);this.sal = sal;}public double getSal() {return sal;}public void setSal(double sal) {this.sal = sal;}@Overridepublic String say() {return super.say()+" sal = "+sal;}public void teach(){System.out.println("teacher = "+getName());}
}//=============================================================================package Person;public class PolyArray {public static void main(String[] args){Person[] persons = new Person[3];persons[0] = new Person("Tom",20);persons[1] = new Student("Jack",30,100);persons[2] = new Teacher("Bob",33,4500);for (int i = 0;i<persons.length;i++){if (persons[i] instanceof Student){((Student)persons[i]).study();}else if (persons[i] instanceof Teacher){((Teacher)persons[i]).teach();}else if (persons[i] instanceof Person){}else{System.out.println("Error");}}}
}

多态参数

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

01:

package TestPolyDemo;public class Employee {private String name;private double salary;public Employee(String name, double salary) {this.name = name;this.salary = salary;}public double getAnnual(){return 12*salary;}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getSalary() {return salary;}public void setSalary(double salary) {this.salary = salary;}}//=====================================================================package TestPolyDemo;public class Worker extends Employee{public Worker(String name, double salary) {super(name, salary);}public void work(){System.out.println("worker = "+getName()+"working!!!");}@Overridepublic double getAnnual() {return super.getAnnual();}
}//==============================================================================package TestPolyDemo;public class Manager extends Employee{private double bonus;public Manager(String name, double salary, double bonus) {super(name, salary);this.bonus = bonus;}public double getBonus() {return bonus;}public void setBonus(double bonus) {this.bonus = bonus;}public void manage(){System.out.println("manager" + getName()+"is managing");}@Overridepublic double getAnnual() {return super.getAnnual()+bonus;}
}//======================================================================package TestPolyDemo;public class TestWorkDemo {public static void main(String[] args){Worker tom = new Worker("tom",2500);Manager milan = new Manager("milan", 5000, 200000);TestWorkDemo testWorkDemo = new TestWorkDemo();testWorkDemo.showEmpAnnual(tom);testWorkDemo.showEmpAnnual(milan);testWorkDemo.testWork(tom);testWorkDemo.testWork(milan);}public void showEmpAnnual(Employee e){System.out.println(e.getAnnual());}public void testWork(Employee e){if (e instanceof Worker){((Worker) e).work();}else if (e instanceof Manager){((Manager) e).manage();}else{System.out.println("no");}}}

Object类

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

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

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

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

==运算符和equals

01:

package ObjectDemo;public class Equals01 {public static void main(String[] args){A a = new A();A b = a;A c = b;System.out.println(a==c);//trueSystem.out.println(b==c);//trueB obj = a;System.out.println(obj==c);//true 地址还是一样"hello".equals("abc");//String equals
//        public boolean equals(Object anObject) {
//        if (this == anObject) {
//            return true;
//        }
//        if (anObject instanceof String) {
//            String aString = (String)anObject;
//            if (!COMPACT_STRINGS || this.coder == aString.coder) {
//                return StringLatin1.equals(value, aString.value);
//            }
//        }
//        return false;//  }//Object equals//        public boolean equals(Object obj) {
//        return (this == obj);
//    }Integer integer1 = new Integer(1000);Integer integer2 = new Integer(1000);System.out.println(integer1 == integer2);//falseSystem.out.println(integer1.equals(integer2));//trueString str1 = new String("hspedu");String str2 = new String("hspedu");System.out.println(str1==str2);//falseSystem.out.println(str1.equals(str2));//true//   Integer equals//        public boolean equals(Object obj) {
//        if (obj instanceof Integer) {
//            return value == ((Integer)obj).intValue();
//        }
//        return false;//   }}
}class B{}class A extends B
{}

如何重写equals方法

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

01:

package EqualsExercise01;public class EqualsExercise01 {public static void main(String[] args){Person person1 = new Person("jack", 10, '男');Person person2 = new Person("jack", 10, '男');System.out.println(person1.equals(person2));}
}class Person
{private String name;private int age;private char gender;public boolean equals(Object obj){if (this==obj){return true;}if (obj instanceof Person){Person p = (Person)obj;return this.name.equals(p.name) && this.age== p.age && this.gender ==p.gender;}return false;}public Person(String name, int age, char gender) {this.name = name;this.age = age;this.gender = gender;}
}

小练习

01:

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

02:

public class WorkDemo {public static void main(String[] args) {int it = 65;float fl = 65.0f;System.out.println("65和65.0f是否相等?"+(it==fl));//truechar ch1 = 'A';char ch2 = 12;System.out.println("65和'A'是否相等?"+(it==ch1));//trueSystem.out.println("12和ch2是否相等?"+(12==ch2));//trueString str1 = new String("hello");String str2 = new String("hello");System.out.println((str1==str2));//falseSystem.out.println(str1.equals(str2));//true}
}

hashCode方法

在这里插入图片描述

在这里插入图片描述

toString方法

01:

package ObjectWorkDemo;public class TestDemo02 {public static void main(String[] args) {//   Object的toString()源码
//    public String toString() {
//        return getClass().getName() + "@" + Integer.toHexString(hashCode());
//    }Monster monster = new Monster("Tom", "climb", 1000);System.out.println(monster.toString()+" "+monster.hashCode());//ObjectWorkDemo.Monster@776ec8df 2003749087//Monster{name='Tom', job='climb', sal=1000.0} 1072408673System.out.println(monster);//Monster{name='Tom', job='climb', sal=1000.0}}}class Monster
{private String name;private String job;private double sal;public Monster(String name, String job, double sal) {this.name = name;this.job = job;this.sal = sal;}@Overridepublic String toString() {return "Monster{" +"name='" + name + '\'' +", job='" + job + '\'' +", sal=" + sal +'}';}
}

finalize方法

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

01:

package FinalizeDemo;public class FinalizeDemo {public static void main(String[] args){Car Bwm = new Car("Baoma");Bwm = null;System.gc();}
}class Car
{private String name;public Car(String name) {this.name = name;}@Overrideprotected void finalize() throws Throwable {System.out.println("Car destory!");}
}

大练习

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

01:

package HomeWorkDemo;public class HomeWorkDemo01 {public static void main(String[] args){Person[] persons = new Person[3];persons[0] = new Person("Tom",35,"JavaEE工程师");persons[1] = new Person("jack",18,"C++工程师");persons[2] = new Person("mike",89,"Teacher");for (int i = 0;i<persons.length-1;i++){for (int j = 0;j<persons.length-1-i;j++){if (persons[j].age < persons[j+1].age){Person tmp = persons[j];persons[j] = persons[j+1];persons[j+1] = tmp;}}}for (int i = 0;i<persons.length;i++){System.out.println(persons[i]);}}
}class Person
{private String name;public int age;private String job;public Person(String name, int age, String job) {this.name = name;this.age = age;this.job = job;}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", age=" + age +", job='" + job + '\'' +'}';}
}

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

02:

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

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

03:

package HomeWorkDemo01;public   class Teacher{private String name;private int age;private String post;private double salary;private double grade;public Teacher(String name, int age, String post, double salary, double grade) {this.name = name;this.age = age;this.post = post;this.salary = salary;this.grade = grade;}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;}public String getPost() {return post;}public void setPost(String post) {this.post = post;}public double getSalary() {return salary;}public void setSalary(double salary) {this.salary = salary;}public double getGrade() {return grade;}public void setGrade(double grade) {this.grade = grade;}public void introduce(){System.out.println(toString());}@Overridepublic String toString() {return "Teacher{" +"name='" + name + '\'' +", age=" + age +", post='" + post + '\'' +", salary=" + salary +", grade=" + grade +'}';}}//=================================================package HomeWorkDemo01;public class Professor extends Teacher{public Professor(String name, int age, String post, double salary, double grade) {super(name, age, post, salary, grade);}@Overridepublic void introduce() {System.out.println("this is professor");super.introduce();}
}//===================================================package HomeWorkDemo01;public class HomeWork03 {public static void main(String[] args){Professor professor = new Professor("贾宝玉", 30, "高级职称", 30000, 1.3);professor.introduce();}
}

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

04:

package HomeWork04;public class Employee {private String name;private double daySal;private int workDays;private double grade;public void printSal(){System.out.println(name+" Sal = "+daySal*workDays*grade);}public Employee(String name, double daySal, int workDays, double grade) {this.name = name;this.daySal = daySal;this.workDays = workDays;this.grade = grade;}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getDaySal() {return daySal;}public void setDaySal(double daySal) {this.daySal = daySal;}public int getWorkDays() {return workDays;}public void setWorkDays(int workDays) {this.workDays = workDays;}public double getGrade() {return grade;}public void setGrade(double grade) {this.grade = grade;}
}//==================================================package HomeWork04;public class Manager extends Employee{private double bonus;public Manager(String name, double daySal, int workDays, double grade) {super(name, daySal, workDays, grade);}@Overridepublic void printSal() {System.out.println("name"+getName()+" Sal = "+(bonus+getDaySal()*getWorkDays()*getGrade()));}public double getBonus() {return bonus;}public void setBonus(double bonus) {this.bonus = bonus;}
}//==================================================package HomeWork04;public class Worker extends Employee{public Worker(String name, double daySal, int workDays, double grade) {super(name, daySal, workDays, grade);}@Overridepublic void printSal() {super.printSal();}
}//==================================================package HomeWork04;public class HomeWork04 {public static void main(String[] args){Manager manage= new Manager("刘备", 100, 20, 1.2);manage.setBonus(3000);manage.printSal();Worker worker = new Worker("关羽",50,10,1.0);worker.printSal();}}

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

05:

package HomeWork05;public class Worker extends Employee{public Worker(String name, double sal) {super(name, sal);}@Overridepublic void printSal() {System.out.println("Worker = ");super.printSal();}
}//==============================================package HomeWork05;public class Employee {private String name;private double sal;private int salMonth = 12;public Employee(String name, double sal) {this.name = name;this.sal = sal;}public void printSal(){System.out.println(name+" Sal = "+(sal*salMonth));}public String getName() {return name;}public void setName(String name) {this.name = name;}public double getSal() {return sal;}public void setSal(double sal) {this.sal = sal;}public int getSalMonth() {return salMonth;}public void setSalMonth(int salMonth) {this.salMonth = salMonth;}
}//============================================package HomeWork05;public class Peasant extends Employee{public Peasant(String name, double sal) {super(name, sal);}@Overridepublic void printSal() {System.out.println("Peasant = ");super.printSal();}
}//============================================package HomeWork05;public class Teacher extends Employee{private int classDays;private double classSal;@Overridepublic void printSal() {System.out.println("name = "+getName()+" Sal = "+(getSal()*getSalMonth()+classDays * classSal));}public Teacher(String name, double sal) {super(name, sal);}public int getClassDays() {return classDays;}public void setClassDays(int classDays) {this.classDays = classDays;}public double getClassSal() {return classSal;}public void setClassSal(double classSal) {this.classSal = classSal;}
}//===============================================package HomeWork05;public class Scientist extends Employee{private double bonus;public Scientist(String name, double sal) {super(name, sal);}@Overridepublic void printSal() {System.out.println("Scientist = "+getName()+" Sal = "+getSal()*getSalMonth()+bonus);}public double getBonus() {return bonus;}public void setBonus(double bonus) {this.bonus = bonus;}
}//============================================package HomeWork05;public class HomeWork05 {public static void main(String[] args){Worker jack = new Worker("jack",1000);jack.printSal();Peasant smith = new Peasant("smith", 20000);smith.printSal();Teacher teacher = new Teacher("顺平", 2000);teacher.setClassDays(360);teacher.setClassSal(5000);teacher.printSal();Scientist scientist = new Scientist("钟南山", 20000);scientist.setBonus(2000000);scientist.printSal();}
}

06:

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

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

07:

package HomeWork07;public class Test {String name = "Rose";Test(){System.out.println("Test");}Test(String name){this.name = name;}}class Demo extends Test{String name = "jack";Demo(){super();System.out.println("Demo");}Demo(String s){super(s);}public void test(){System.out.println(super.name);System.out.println(this.name);}public static void main(String [] args){new Demo().test();new Demo("join").test();}}//    Test
//    Demo
//    Rose
//    jack
//    join
//    jack

08:

查看JDK源码

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

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

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

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/309148.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[Mvp.Blazor] 动态路由与钩子函数

&#xff08;Blazor组件的生命周期函数&#xff09;一直在学习也没有停下脚步&#xff0c;用着脑子还是挺好的&#xff0c;感觉可以更脚踏实地一下。最近偶尔也继续看了看Blazor&#xff0c;毕竟我也开源了一个项目嘛&#xff0c;基本我正式开源的项目都会负责到底&#xff0c;…

Java面向对象编程(高级)

面向对象编程(高级) 类变量和类方法 01: package ChildDemo;public class Child {private String name;public static int cnt 0;public Child(String name){this.name name;}public void join(){System.out.println(name "join the game");} }//package ChildDe…

.NET Core + Kubernetes:Volume

和 Docker 类似&#xff0c;Kubernetes 中也提供了 Volume 来实现数据卷挂载&#xff0c;但 Kubernetes 中 Volume 是基于 Pod&#xff0c;而不是容器&#xff0c;它可被 Pod 中多个容器共享&#xff0c;另外 Kubernetes 中提供比较丰富的 Volume 类型[1]&#xff0c;如&#…

WPF中的Data Binding调试指南

点击蓝字“大白技术控”关注我哟加个“星标★”&#xff0c;每日良时&#xff0c;好文必达&#xff01;WPF中的Data Binding如何Debug?大家平时做WPF开发&#xff0c;相信用Visual studio的小伙伴比较多。XAML代码曾经在某些特殊版本的Visual Studio中是可以加断点进行调试的&…

.NET 5 尝鲜 - 开源项目TerminalMACS WPF管理端支持.NET 5

点击上方“Dotnet9”添加关注哦聊天界面设计TerminalMACS一个使用 Prism 作为模块化框架、基于多个开源控件库作为UI控件选择、集成开源 UI 界面设计的 .NET 5 WPF 客户端项目。项目名称&#xff1a;TerminalMACS WPF管理端项目开源地址&#xff1a;Github&#xff1a;https://…

FreeSql.Generator命令行代码生成器是如何实现的

目录FreeSql介绍FreeSql.GeneratorRazorEngine.NetCore源码解析FreeSql.ToolsFreeSqlFreeSql 是功能强大的对象关系映射技术(O/RM)&#xff0c;支持 .NETCore 2.1 或 .NETFramework 4.0 或 Xamarin。有一个强大的ORM&#xff0c;也方便我们开发一个代码生成器。一般情况下&…

Java IDEA断点调试

断点调试(debug) 断点调试应用案例 01&#xff1a; package Assign;public class Debug01 {public static void main(String[] args) {int sum 0;for (int i 0;i<5;i){sumi;System.out.println(i);System.out.println(sum);}System.out.println("continue");} …

.NET Core请求控制器Action方法正确匹配,但为何404?

【导读】提前预祝各位端午节快乐。有时候我们会发现方法名称都正确匹配&#xff0c;但就是找不到对应请求接口&#xff0c;所以本文我们来深入了解下何时会出现接口请求404的情况。匹配控制器Action方法&#xff08;404&#xff09;首先我们创建一个web api应用程序&#xff0c…

布斯乘法以及带符号数的运算

乘法理解 对于最熟悉的十进制乘法很最基本的运算原理就是一个乘数乘以另一个乘数的个位、十位、百位数字然后求和。比如 放到二进制来看其实它也是这样的&#xff0c;多位数的乘法就是一个乘数乘上另一个乘数的各位求和。那么&#xff1a; 布斯算法及原理 原理 已经知道两…

angular 接入 IdentityServer4

angular 接入 IdentityServer4Intro最近把活动室预约的项目做了一个升级&#xff0c;预约活动室需要登录才能预约&#xff0c;并用 IdentityServer4 做了一个统一的登录注册中心&#xff0c;这样以后就可以把其他的需要用户操作的应用统一到 IdentityServer 这里&#xff0c;这…

主机Redis服务迁移到现有Docker Overlay网络

“《麻雀虽小&#xff0c;五脏俱全》之主机现有Redis服务迁移到Docker Swarm Overlay网络&#xff0c;并搭建高可用容器集群。hello, 好久不见&#xff0c;之前文章记录了一个实战的2C分布式项目的改造过程&#xff0c;结果如下&#xff1a;其中Redis并未完成容器化改造&#x…

Java控制结构

控制结构 程序流程控制介绍 顺序控制 分支控制if-else 单分支 案例演示 01: import java.util.Scanner; public class IfWorkDemo {public static void main(String[] args){Scanner myScanner new Scanner(System.in);System.out.println("input your age");int…

.Net Core Configuration源码探究

前言上篇文章我们演示了为Configuration添加Etcd数据源&#xff0c;并且了解到为Configuration扩展自定义数据源还是非常简单的&#xff0c;核心就是把数据源的数据按照一定的规则读取到指定的字典里&#xff0c;这些都得益于微软设计的合理性和便捷性。本篇文章我们将一起探究…

面试官:你说你喜欢研究新技术,那么请说说你对 Blazor 的了解

阅读本文大概需要 1.5 分钟。最近在几个微信 .NET 交流群里大家讨论比较频繁的话题就是这几天自己的面试经历。面试官&#xff1a;“你刚说你喜欢研究新技术&#xff0c;那么你对 Blazor 了解多少&#xff1f;”作为一位专注于 .NET 开发的软件工程师&#xff0c;你好意思说你对…

Java变量

变量 ​ 变量是程序的基本组成单位 变量的介绍 概念 变量相当于内存中一个数据存储空间的表示&#xff0c;你可以把变量看做是一个房间的门牌号&#xff0c;通过门牌号我们可以找到房间&#xff0c;而通过变量名可以访问到变量(值)。 01&#xff1a; class Test {public s…

[Student.Achieve] 学生教务管理系统开源

&#xff08;源自&#xff1a;https://neters.club&#xff09;一觉醒来Github改版了&#xff0c;我个人还是挺喜欢的????。还有两个月就是老张做开源两周年了&#xff0c;时间真快&#xff0c;也慢慢的贡献了很多的开源作品&#xff0c;上边的是主要的七个作品&#xff0c…

.NET Core HttpClient源码探究

前言在之前的文章我们介绍过HttpClient相关的服务发现&#xff0c;确实HttpClient是目前.NET Core进行Http网络编程的的主要手段。在之前的介绍中也看到了&#xff0c;我们使用了一个很重要的抽象HttpMessageHandler&#xff0c;接下来我们就探究一下HttpClient源码&#xff0c…

Java 多线程:线程优先级

1 优先级取值范围 Java 线程优先级使用 1 ~ 10 的整数表示&#xff1a; 最低优先级 1&#xff1a;Thread.MIN_PRIORITY 最高优先级 10&#xff1a;Thread.MAX_PRIORITY 普通优先级 5&#xff1a;Thread.NORM_PRIORITY 2 获取线程优先级 public static void main(String[]…

《Unit Testing》1.1 -1.2 单元测试的目的

本系列是《Unit Testing》 一书的读书笔记 精华提取。书中的例子 C# 语言编写&#xff0c;但概念是通用的&#xff0c;只要懂得面向对象编程就可以。 单元测试当前的状态目前&#xff0c;在&#xff08;美国的&#xff09;大部分公司里&#xff0c;单元测试都是强制性的。生产…

Java Exception

Exception 异常捕获 将代码块选中->ctrlaltt->选中try-catch 01: public class Exception01 {public static void main(String[] args) {int n1 10;int n2 0;try {int res n1/n2;} catch (Exception e) { // e.printStackTrace();System.out.println(e.…