版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_42528266/article/details/102949627
代码示例
package com.cwl.base.day03.oop;
/**
* @program: java_base
* @description:
* @author: ChenWenLong
* @create: 2019-11-07 10:42
**/
public class TestExtends {
public static void main(String[] args) {
Student stu = new Student();
stu.name="ChenWenLong";
stu.height = 172;
stu.rest();
Student stu2 = new Student("希希",6,"挖掘机专业");
System.out.println(stu2 instanceof Student);
System.out.println(stu2 instanceof Person );
System.out.println(stu2 instanceof Object );
System.out.println(new Person() instanceof Student );
}
}
class Person /*extends Object*/ {
String name;
int height;
public void rest(){
System.out.println("休息一会!");
}
}
class Student extends Person {
String major;
public void study(){
System.out.println("学习两小时!");
}
public Student(String name,int height, String major){
this.name = name;
this.height = height;
this.major = major;
}
public Student(){
}
}
代码示例
package com.cwl.base.day03.oop;
/**
* @program: java_base
* @description:
* @author: ChenWenLong
* @create: 2019-11-07 10:39
**/
public class TestEquals {
public static void main(String[] args) {
Object obj;
String str;
User u1 = new User(1000,"***","123456");
User u2 = new User(1000,"***","123456");
System.out.println(u1==u2);
System.out.println(u1.equals(u2));
String str1 = new String("***");
String str2 = new String("***");
System.out.println(str1==str2); //false
System.out.println(str1.equals(str2)); //true
}
}
class User {
int id;
String name;
String pwd;
public User(int id, String name, String pwd) {
super();
this.id = id;
this.name = name;
this.pwd = pwd;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (id != other.id)
return false;
return true;
}
}