Niuke.com Wrong Question Collection 6

1.length is the length of the array

int []a={1,2,3,3};
int b=a.length;

length() is the string length

String c="fasdfasdfla";
c.length();

size() is the set length

2. In a static method, only the static members in the method of this class can be accessed, and non-static properties and methods cannot be directly accessed.

This is because the static method does not depend on the object, so when the class is loaded successfully, the static method can be accessed. At this time, the object does not necessarily exist, and the non-static member does not necessarily exist. The this keyword cannot appear in static methods, because this is for the object. Static methods in this class can access the static properties of this class, and can also call static methods.

The value of a static data member can be modified. For example: 
static int a = 1;
I can continue to write:
a = 2;
There is no problem. It's just that the area where the static data is stored is different.
Unmodifiable are constants.
3.
class A {
    public A(String str) {
    }
}

class Test {
    public static void main(String[] args) {
        A classa = new A("he");
        A classb = new A("he");
        System.out.println(classa == classb);
        System.out.println(classa.equals(classb));
    }
}

The answer is all false

equals is also wrong because the subclass does not override the equals() method of Object, and calls '==' by default to compare

To determine whether two objects are equal, you need to override the equals() method and the hashcode() method

The source code of object.equals is as follows:

public boolean equals(Object obj) {
        return (this == obj);
    }

4.

 

class A {
    public int func1(int a, int b) {
        return a - b;
    }
}

class B extends A {
    public int func1(int a, int b) {
        return a + b;
    }
}

public class ChildClass {
    public static void main(String[] args) {
        A a = new B();
        B b = new B();
        System.out.println("Result=" + a.func1(100, 50));
        System.out.println("Result=" + b.func1(100, 50));
    }
}
Answer: 150, 150



  For polymorphism, it can be summarized as:        


     1. Use the reference of the parent class type to point to the object of the subclass; 


    2. The reference can only call the methods and variables defined in the parent class; 


    3. If the parent class is rewritten in the subclass A method in the subclass, then when this method is called, the method in the subclass will be called; (dynamic connection, dynamic call) 


    4. Variables cannot be overridden (overridden), and the concept of "override" is only for methods , if the variable in the parent class is "overridden" in the subclass, an error will be reported at compile time. 


Three necessary conditions for polymorphism: 


        1. Inheritance 2. Override 3. The parent class reference points to the child class object. 




Upcasting: Person p = new Man() ; //Upcasting does not require casts 


Downcast: Man man = (Man)new Person() ; //must cast typecast 

The key is here, no matter whether it is up or down, it is a sentence, "compile to the left, run to the right". That is, when compiling, it will see whether the reference type on the left can be compiled correctly, and when running, it will call the method of the object on the right. 


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325726751&siteId=291194637