30 days to get Java - day13

A daily test and review

Day test

  1. What is polymorphism? What is a virtual method call?
多态性:父类的引用指向子类的对象
虚拟方法调用:编译看左,运行看右
  1. A class can have several direct parent? How many sub-class may have a parent class? Subclasses can get the direct parent structure of the parent class in it? Subclass can acquire properties or methods of the parent class private rights?
一个类可以有一个直接父类
一个父类可以有多个子类
子类能获取直接父类的父类中的结构
子类能获取父类中private权限的属性或方法
  1. Specific rules rewriting (override / overwrite) methods are there?
方法名、参数列表相同
子类方法权限小于等于父类方法
子类的返回类型必须是父类方法返回类型的子类或与其相同
  1. super constructor call, what specific points of attention
须放在构造器的首行
参数列表和父类中定义的相同

Review
day12 learning content

Object-Oriented

The third object-oriented features: polymorphism (contact Day12)

Object type conversion

Man is a subclass of Person, Person person = new Man()Person not call Man Person class has no way to call at this time need For cast (downcast)Man man = (Man) person

Upward transition is polymorphic

Cast will have problems, it may be error java.lang.ClassCastExceptionThis type of error is caused by the conversion of

instanceof

a instanceof A: determining whether the object is an instance of a class A. If so, it returns true; if not, it returns false

To avoid downcast when java.lang.ClassCastExceptionabnormal, we downcast before the first judge instanceof, once returns true, would be downcast. If it returns false, do not be downcast

Polymorphism Classic title

package com.atguigu.exer;

//考查多态的笔试题目:
public class InterviewTest1 {

	public static void main(String[] args) {
		Base1 base = new Sub1();
		base.add(1, 2, 3);   // sub_1

		Sub1 s = (Sub1) base;
		s.add(1, 2, 3);      // sub_2

	}
}

class Base1 {
	public void add(int a, int... arr) {
		System.out.println("base1");
	}
}

class Sub1 extends Base1 {

	public void add(int a, int[] arr) {
		System.out.println("sub_1");
	}

	public void add(int a, int b, int c) {
		System.out.println("sub_2");
	}
}

base can only be called the beginning of the class Base1 some method (see compilation left), only two possibilities (output sub_1 compiled or not), if you think the output base1 recommend see more and more states; here think subclass method override methods in parent classes

Use the Object class

  • Object class is the root class of all Java classes
  • If not specified parent class extends keyword in the class declaration, the default parent class to class java.lang.Object

The difference between == and the equals method

A, ==: Operator

  1. You may be used in the basic variable data types and the reference data type variable
  2. If the comparison is the basic data types of variables: whether to save the data to compare two variables are equal. (Not necessarily to the same type)
    if the comparison variable is a reference data type: address value to compare two objects are the same; that is, whether two object references point to the same entity
    added: when == symbols, must ensure that the left and right sides of the symbol consistent with the variable type.

Second, the use equals () methods:
3. is a method, rather than the operator
4 can only be applied to the reference data type
definitions 5. Object class equals () is:

public boolean equals(Object obj) {
    return (this =\= obj);
}
 说明:```Object类中定义的equals()和==的作用是相同的:比较两个对象的地址值是否相同;即两个引用是否指向同一个对象实体```
  1. Like String, Date, File, packaging and so overrides equals () method of the Object class. After the rewrite, the address is not comparing two references are the same, but the "real content" to compare two objects are the same.

  2. Under normal circumstances, our custom classes if you use equals (), it is also usually the "real content" to compare two objects are the same. So, we need to equals Object class () rewrite.
    Principles rewritten: Entity compare the contents of two objects are the same.

Object class toString () is used:

  1. When we export a reference to the object, in fact, called the current object's toString ()

  2. In toString () Object class definition:

    publicString toString() {
       return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }
    
  3. Like String, Date, File, packaging and so overrides the toString () method of the Object class.
    So call the object's toString (), the return to "real content" information

  4. Custom class can override toString () method, when this method is called, the object returns "substantial contents"

Use wrapper class

  • Reference types for the corresponding eight basic data type definitions - packaging (package type)
  • With the method of class characteristics, you can call the class, Java is the real object-oriented
Basic data types Wrapper class
byte Byte
short Short
int Integer
long Long
float Float
double Double
boolean Boolean
char Character
  • Examples of the basic data types packed into the packaging - packing
    achieved by configuring wrapper class:
int i = 500; 
Integer t = new Integer(i);
  • Objects can also be constructed by packaging the string argument:
Float f = new Float(4.56);
  • Obtaining an object packaged in packaging basic types of variables - unpacking
    call .xxxValue wrapper class () method:
boolean b = bObj.booleanValue();

After JDK1.5, support for automatic packing, automatic unpacking . But the type must match

@Test
public void test3() {
	int num1 = 10;
	// 基本数据类型-->包装类的对象
	method(num1);

	// 自动装箱:基本数据类型 --->包装类
	int num2 = 10;
	Integer in1 = num2;// 自动装箱

	boolean b1 = true;
	Boolean b2 = b1;// 自动装箱

	// 自动拆箱:包装类--->基本数据类型
	System.out.println(in1.toString());

	int num3 = in1;// 自动拆箱
}

And packaging string interconversion

//String类型 --->基本数据类型、包装类:调用包装类的parseXxx(String s)
@Test
public void test5(){
	String str1 = "123";
	int num2 = Integer.parseInt(str1);
	System.out.println(num2 + 1);
	
	String str2 = "true1";
	boolean b1 = Boolean.parseBoolean(str2);
	System.out.println(b1);
}

//基本数据类型、包装类--->String类型:调用String重载的valueOf(Xxx xxx)
@Test
public void test4(){
	
	int num1 = 10;
	//方式1:连接运算
	String str1 = num1 + "";
	//方式2:调用String的valueOf(Xxx xxx)
	float f1 = 12.3f;
	String str2 = String.valueOf(f1);//"12.3"
}

Packaging Classic title

@Test
public void test3() {
	Integer i = new Integer(1);
	Integer j = new Integer(1);
	System.out.println(i == j);//false
	
	//Integer内部定义了IntegerCache结构,IntegerCache中定义了Integer[],
	//保存了从-128~127范围的整数。如果我们使用自动装箱的方式,给Integer赋值的范围在
	//-128~127范围内时,可以直接使用数组中的元素,不用再去new了。目的:提高效率
	
	Integer m = 1;
	Integer n = 1;
	System.out.println(m == n);//true

	Integer x = 128;//相当于new了一个Integer对象
	Integer y = 128;//相当于new了一个Integer对象
	System.out.println(x == y);//false
}
Published 22 original articles · won praise 34 · views 7442

Guess you like

Origin blog.csdn.net/weixin_42224119/article/details/105249691