class Subclass extends SuperClass{
}
在子类中可以根据需要对从父类中继承来的方法进行改造,也称为方法的重置、覆盖。在程序执行时,子类的方法将覆盖父类的方法。
class Person{
private void name() {
}
}
class Stu extends Person{
//@Override //父类的方法是private,不能重写
public void name(){
}
}
class Person{
public void name() {
}
}
class Stu extends Person{
// @Override //子类比父类权限小,编译不通过
// void name(){
// }
}
long g=20;
double d=12.0f
float f=(float)12.0;
int a=(int)1200L
==操作符
int a=5;
if(a==6){…}
Person p1=new Person();
Person p2=new Person();
if (p1==p2){…}
public class App {
public static void main(String[] args) {
Person person = new Person();
Stu stu = new Stu();
if (stu==person) {//报错:Incompatible operand types Stu and Person
System.out.println(1);
}
}
}
class Person{
}
class Stu{
}
equals()
重写equals()方法的原则
public static void main(String[] args) {
int it = 65;
float fl = 65.0f;
System.out.println("65和65.0f是否相等?" + (it == fl)); //true,inter自动提升为float类型
char ch1 = 'A';
char ch2 = 12;
System.out.println("65和'A'是否相等?" + (it == ch1));//true
System.out.println("12和ch2是否相等?" + (12 == ch2));//true
String str1 = new String("hello");
String str2 = new String("hello");
System.out.println("str1和str2是否相等?"+ (str1 == str2));//false
System.out.println("str1是否equals str2?"+(str1.equals(str2)));//true
}
public class App {
public static void main(String[] args) {
Stu stu1 = new Stu("tom", 1);
Stu stu2 = new Stu("john", 2);
System.out.println(stu1.equals(stu2)); //false
}
}
class Stu {
private String name;
private int ids;
public Stu(String name, int ids) {
this.name = name;
this.ids = ids;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getIds() {
return ids;
}
public void setIds(int ids) {
this.ids = ids;
}
@Override
public boolean equals(Object obj) {
if(this==obj)
return true;
if (obj==null)
return false;
if (obj instanceof Stu) {
Stu other=(Stu)obj;
if (this.ids == other.getIds()) {
return true;
}
}
return false;
}
}
public class App {
public static void main(String[] args) {
MyDate m1 = new MyDate(14, 3, 1976);
MyDate m2 = new MyDate(14, 3, 1976);
Object m3=new MyDate(22, 1, 2020);
System.out.println(m1.getClass()+"-----"+m3.getClass());//class MyDate-----class MyDate
if (m1 == m2) {
System.out.println("m1==m2");
} else {
System.out.println("m1!=m2"); // m1 != m2
}
if (m1.equals(m2)) {
System.out.println("m1 is equal to m2");// m1 is equal to m2
} else {
System.out.println("m1 is not equal to m2");
}
}
}
class MyDate {
private int days;
private int month;
private int year;
public MyDate(int days, int month, int year) {
this.days = days;
this.month = month;
this.year = year;
}
@Override
public boolean equals(Object obj) {
if(obj == null)
return false;
if(obj == this)
return true;
if(obj instanceof MyDate){
MyDate date= (MyDate) obj;
if (date.month== month && date.year==year && date.days==days) {
return true;
}
}
return false;
}
}
Date now=new Date();
System.out.println(“now=”+now);
//相当于
System.out.println(“now=”+now.toString());
s1=“hello”;
System.out.println(s1);//相当于System.out.println(s1.toString());
int a=10;
System.out.println(“a=”+a);
public class App {
public static void main(String[] args) {
Circle c1 = new Circle(1,"red",2);
Circle c2 = new Circle(1,"red",2);
System.out.println(c1.equals(c2));
System.out.println(c1);
c2 = new Circle(2,"black",2);
System.out.println(c1.equals(c2));
System.out.println(c2);
}
}
class Circle extends GeometricObject{
private double radius;
public Circle(){
super();
radius=1.0;
}
public Circle(double radius){
super();
this.radius=radius;
}
public Circle(double radius,String color,double weight){
super(color,weight);
this.radius=radius;
}
public double findArea(){
return Math.PI*radius*radius;
}
public boolean equals(Object obj){
if(obj==null)
return false;
if(obj == this)
return true;
if (obj instanceof Circle) {
Circle cir=(Circle)obj;
if (cir.radius==radius) {
return true;
}
}
return false;
}
public String toString(){
return "圆的半径是"+radius;
}
}
class GeometricObject{
protected String color;
protected double weight;
protected GeometricObject(){
color="white";
weight=1.0;
}
protected GeometricObject(String color, double weight){
this.color=color;
this.weight=weight;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
}
基本数据类型 | 包装类 |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
boolean | Boolean |
char | Character |
int i = 500;
Integer t = new Integer(i);
Float f = new Float("4.56");
Long l = new Long("asdf"); //NumberFormatException
Integer i=new Integer(1);
int y=i.intValue();
System.out.println(y);//1
int i = new Integer("12");
Float f = Float.parseFloat("12.1");
String str = String.valueOf(2.34f);
String intStr = 5 + "";
Object o1 = true ? new Integer(1) : new Double(2.0);
System.out.println(o1);//1.0,因为要保持一致,1自动提升为1.0
Object o2;
if (true)
o2 = new Integer(1);
else
o2 = new Double(2.0);
System.out.println(o2);//1
Integer i = new Integer(1);
Integer j = new Integer(1);
System.out.println(i == j);//false
Integer m = 1;
Integer n = 1;
System.out.println(m == n);//true
Integer x = 128;
Integer y = 128;
System.out.println(x == y);//false