(Original) Foot-headed Java: Basic Articles

xiaoxiao2021-03-06  61

(Original) Personal Land Java: Basic Article Recently chatted with a few friends, when talking about the techniques used by their respective Java to do projects, praise, I don't know how cloud, like the speech before the President Bush is generally wonderful, I am in the fog, a fog! But when I ask them some basic Java language mechanisms, when some basic thinking of objects, I replied that the expression when I swallowed and the "speech" was walked! I wonder, "There is no more than ten minutes before and after the conversation, this gap is so big !! Haha !!!". Ok, don't go, talk about the topic. The upper side said so much, meaning, learning is to do things like people, down, one step, one step, you can't have to be half a point on the road of learning technology, and there is no so-called shortcut to go, if you walk, I believe you One day is still going again. This is the purpose of I wrote this article to help those who are learning or using Java to master technical details, and lay a good foundation. Note: The first few articles of this series uses instances to explain some basic concepts, and the Java mechanism will continue to be inseparable from the sections. If you have been working for a few years, I will help me pick the wrong, and I am grateful! ! ! If you are a beginner Java, I suggest you use the instance of me with your understanding, if there is a technical problem to discuss. My email address: Billzhangmingmging@sina.com.cn

(1) Equals and == comparisons (recognized as the most commonly asked question at the Java Interview): Primitive Type Storage In the stack, it is not an object type, so it is impossible to include Equals method in the primitive type. For basic types, there is no way to presence or later, there is no way to send messages to the object.

1) Use "==" to compare whether the two basic (primitive) type value is equal:

/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------- * / public class Testequals {public static void main (string [] args) {INT i = 5, j = 3; system.out.println (i == J? "The Object Equals !!": "The Object UNEQUALS !!!!! !!!!!!! ");}} / * ------------------------------------ -------------------------------------------------- -------- * / Result: The Object UNEQUALS !!!!!!!!!!!! The opposite, if the value of the two variables is equal, then output is the object equals !!

2) Use "==" to compare whether the two object references are equal:

/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------- * / Class Cup {} public class objectcompare {public static void main (string [] args) {CUP CUP1 = New CUP (); CUP CUP2 = New CUP (); System.out.Println (Cup1 == Cup2? "The Object Reference Equals" : "The Object Reference Unequals");}} / * ------------------------------------- -------------------------------------------------- ------- * / Result: The Object Reference Unequals3) Use the Equals method (all objects in Java) compare two objects (logical) as the same:

/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------- * / Class A {INT i = 5; PUBLIC Boolean Equals (Final Object Object) {RETURN (((b) Object) .i == i);}} Class B {INT I = 5; Public Boolean Equals (Final Object Object) {Return ((a) Object) .i == i);}} public class testequa {public static void main (string args [) {aa = new a (); bb = new b (); system.out.println (B. Equals (a)? "Equals !!!!!!!!!!": "unequals");}} / * ------------------- -------------------------------------------------- ------------------------- * /

Result: equals !!!!!!!!!! These two objects are equal to customity (as long as the value of variable I in the two objects is equal), that is, equal conditions It is defined by the user based on the specific business needs.

(2) Interface and inheritance:

Interface: The interface defines the type form of the subclass of it, which includes the name of the method, the return value of the method, the parameter list, Java does not support multiple inheritance, to implement multiple inheritance of C , use Go to the interface (because you can implement multiple interfaces, the interface can inherit the interface at the same time, of course, there is another way to achieve multiple inheritance, in the future article will be introduced one by one). There have been such a statement that object-oriented programming is the interface-oriented programming. This sentence is enough to explain the important position of the interface in the object-oriented concept. I will explain in detail in the future article. Here is just a brief introduction to the concept of an interface.

EXTENDS: If a class is inherited from another, you can say: This class is the class that is inherited, usually inherited is called superclas, and inherited class is a subclass (Subclass), with SUBCLASS At the same time, subclasses can expand and overwrite superclars and properties. Class TestInterorextends implements interface B and abstract class A, interfaces and abstract classes define an Event method, which will not conflict in TestInterorextends. / * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------- * / Abstract Class A {public void Event () {system.out.println ("The Abstract Class Event Method !!!!!!!!!!!!!!");}} Interface b {public void Event (); Class tests b {public void Event () {system.out.println ("The Event of exchange !!!!!!!!!!!!!!!!!!");} public static void main (String agrs []) {new testinterorextends (). Event ();}} / * ---------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------- -------------- * / The result: The Event of exchange !!!!!!!!!!!!!!!!!!

(3) Calcularization sequence: subclasses always prioritize the default constructor of the parent class, the order in which the call is called in the order of inheritance.

/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------- * / Class A {Public a () {system.out.println ("the constructor of a");}} public class testinterorextends extends a {

Public testinterorExtends () {system.out.println;} public static void main (string agrs []) {new testinterorextends ();}} / * ---------- -------------------------------------------------- --------------------------------- * / the result: The constructor of a the constructor of testinterExtends About call order Description, I saw a classic example in the forum a few days ago: / * ---------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------- -------------- / public class test2 extends test1 {{system.out.print ("1");} test2 () {system.out.print ("2"); } Static {system.out.print ("3");} {system.out.print ("4");} public static void main (String [] args) {new test2 ();}} class test1 {test1 () {System.out.Print ("5");} static {system.out.print ("6");}} / * ------------------ -------------------------------------------------- -------------------------- * / Result: 635142 (4) The members in the interface must be all PUBLIC, if not written public, default It is public, such as a member function or method within an interface to declare the Private or Protected, the compiler will generate an error when compiling, if the variable is declared in the interface, they are STA Tic and final.

/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------- * / Interface AAA {Protected void a (); public void b ();} class bb import.Println ("The Implement AAA Of BB");} public void b () {system.out .Println ("THE IMPLEMENTION AAA OF BB's Method");}} public class testmyinter {public static void main (String [] args) {aaa a = new bb (); aa ();}} / * ---- -------------------------------------------------- ---------------------------------------- / Result: ------ -------------- Configuration: JDK VERSION ------------------ f: / homework / java / testmyinter. Java: 3: Modifier Protected Void A (); ^ 1 Error <5> Type Conversion: Downcasting is insecure such as: / * ----------- -------------------------------------------------- ------------------------------- * / class a {public void mm () {system.out.println ("mmzhang ");}} Class B Extends a {} public class downcast {public static void main (string" ARGS) {Try {bb = new b (); AA = new a (); ((b) a) .mm ();} catch (exception ex) {ex.printstacktrace ();}}} / * -------------------------------------------------- ----------------------------------------- * /

This translation of the parent class into a subclass can be compiled, but it will generate an exception when Run Time: Run Time Result: java.lang.classcastexception at downcasting.main (Downcasting.java: 19) If the class conversion is converted to upload (Upcasting) is safe because the parent class has all attribute methods of subclasses, but the subclasses not only contain the method properties of the parent class, but the properties and methods of their own expansion, and these methods are not in the parent class. reflect. Therefore, upload is safe.

<6> Class.Forname (String); Use the class name string as a parameter, return the Class's Reference, but in fact, it is not used in the program. Note: Must be caught exception / * ------ -------------------------------------------------- -------------------------------------- * / Class aa {static {system.out.println ("THE Static Method AA");}} class bb {} public class classforname {public static void main (string [] args) {try {class.forname ("aa");} catch (classnotfoundexception ex) {system. Out.println ("THE CLASS NOT FOUND");}}} / * --------------------------------- -------------------------------------------------- ----------- * / <7> Java programming language only passes the value of the parameters! ! ! The programmer of C to Java is easy to confuse this (C has a reference type). When an object instance is transmitted to the method as a parameter, the value of the parameter is a reference to the object (ie, when you pass parameters to a method, the parameter passed to the method is the copy of the object reference). For example: / * --------------------------------------------- ------------------------------------------------------------------- * /

public class TestPara {public static void main (String [] args) {String str = "zhangmingming"; System.out.println ( "The derive string object is:" str); // Define a string object TestPara.changeParameter ( str); System.out.println ( "The string object value is:" str);} public static void changeParameter (String parameter) {parameter = "billzhangmingming"; System.out.println ( "The changed parameter is:" parameter);}} / * ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------- - * /

The derive string object is: zhangmingmingThe changed parameter is: billzhangmingmingThe string object value is: zhangmingmingPress any key to continue ... changeParameter in the method, the reference to pass the incoming object changes, which makes a copy of the object reference points to the other reference Object, so it does not change any changes to the incoming object value. <8> Super Keyword: Refers to the parent class of your own object. It can: (1) Call the parent class constructor (2) call the parent class member or method usage format: super.member (Member can be a function or method)

For example: / * --------------------------------------------- ------------------------------------------------------------------- * / class superclass {string name = "zmm"; superclass () {system.out.println ("Call Super Class Default Construct !!!!!!!");} superclass (int A) {system.out.println "Call Super Class That Has One of Parameter Construct !!!!!!!!!");

} Class ExtendsSuperClass extends SuperClass {String name = "Bill"; ExtendsSuperClass () {super (); // No problem System.out.println ( "The child class construct"); // super (); // call to super . must be first statement in constructor} ExtendsSuperClass (int a) {super (a);} void chanageName (String superName, String subClassName) {super.name = "zhangmingming";. // Invoke super class atrribue and change it this. Name = "Billzhangmingming";} void showmyName () {system.out.println ("The superclass name is:" super.name); System.Out.println ("The Subclass Name is: this.name); }} public class TestSuper {public static void main (String [] args) {ExtendsSuperClass test; test = new ExtendsSuperClass (); int a = 10; test = new ExtendsSuperClass (a); test.showMyName ();}} / * -------------------------------------------------- -------------------------------------------- / NOTICE: (1 If the Super Keyword is written in an ExtendsSUPERCLASS default constructor Print statement, the program generates the following error when compiling: f: /Homework/java/testsuper.java: 17: Call to Su Per Must Be First Statement In Constructor Super (); ^ 1 Error (2) Subclass to hide (overwrite) the parent class name property, but use Super to access the properties of the parent class, no subject's coverage Write the impact. RESULT: Call super class default construct !!!!!!! The child class constructCall super class that has one of parameter construct !!!!!!!!!! The superClass name is: zmmThe subClass name is: BillPress any key to Continue ...

<9> Static Introduction: Static members do not have to rely on class objects, if members in the class are declared as static, then the member (attribute or method) can be accessed before the class creates objects, the STATIC method's classic example is Main method, because main () must first be called first when the program start execution, so it is declared as static. Static functions and variables can be called directly using class names because Static members do not rely on any type of object feature. Such as: classname.staticmember (this feature, can not help but remind me of the classic white "in" Master ":" I am not your second uncle, I am the fairy sent by Yu Emperor, just borrow you Second Uncle The flesh !!! ") You can also use the object name of the static member to be accessed as: obj.staticmember. Variables that are declared STATIC can be regarded as global variables. If a class contains a STATIC variable, when a subject is declared, there is no copy of the Static variable, if the method in the class needs to be declared in your class There are two types of access methods: 1) declare the method as Static. 2) Use the class name of this class to call. That is, the classname.staticMember mentioned above. 3) If you need to pass the Static variable to a method when the parameter is transmitted, the method must be declared to static (this is one of the errors that are often crossed during programming when programming). EXAMPLE: / * -------------------------------------------------------------------------------------------- ----------------------------------------------- * / public class TestStatic {static int i = 10; public static void main (String args []) {StaticClassMember a = new StaticClassMember (); a.method1 (); // Call static member used object name System.out.println (. "THE Static Property Value IS:" TestStatic.i); i = i 5; System.out.Println ("The Static Property Value IS:" TestStatic.i); AAA (i); // Passing Static MEMBER .It's little funny.

} Static void aaa (int a) {}}

Class staticclassmember {static void method1 () {system.out.println ("the static method1");}}

/ * ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------- * /

The Static Method1The Static Property Value is: The Static Property Value IS:

Note: Whether you have created multiple TestStatic, they all use the same I variable because all objects reference the same memory address.

转载请注明原文地址:https://www.9cbs.com/read-84871.html

New Post(0)