In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the example analysis of classes and objects in Java, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.
Class can be seen as a template for creating Java objects
1. Definition of class public class Dog {String name; int age; void eat () {} void sleep () {}} 2, type of variable in class
Local variable: a variable defined in a method or statement block is called a local variable. Variable declaration and initialization are in the method, and after the method ends, the variable is automatically destroyed.
Member variables (instance variables): member variables are defined in the class, outside the body of the method. This variable is instantiated when the object is created. Member variables can be accessed by methods, constructors, and statement blocks of a specific class.
Class variables: class variables are also declared in the class, outside the method body, but must be declared as a static type.
3. Construction method
The name of the constructor must be the same as the class, and a class can have more than one constructor.
At least one constructor must be called when creating an object. If you do not explicitly define a constructor for a class, the Java compiler will provide a default constructor for the class.
Public class Dog {public Dog () {System.out.println ("Construction Dog");} 4, overload method
When you create two methods with the same name and different parameter lists (that is, different number or types of parameters), you have an overloaded method.
At run time, JRE decides which variant of your overloaded method to call based on the parameters passed to it.
We can even call another overloaded method with the same name in one method.
But be careful: when using overloaded methods
You cannot overload a method simply by changing its return type.
You cannot have two methods with the same name and the same parameter list.
5. Inheritance
To indicate that a class inherits from a superclass, we can declare the class by adding extends after className
For example:
Public class Employee extends Person {/ / Employee inherits from Person public Employee () {super ();} public Employee (String name, int age, int height, int weight, String eyeColor, String gender) {super (name, age, height, weight, eyeColor, gender);}}
In the constructor, call super ([args]) to initialize the parent class.
5.1 rewriting method
If a subclass provides its own implementation of a method defined in its parent class, this is called method rewriting.
The form of rewriting is similar to overloading, but you need to add an @ Override. For example, when there is a foo () method in the parent class, we can override it like this in the subclass:
@ Overridepublic int foo () {/ / TODO Auto-generated method stub return super.foo ();}
[note] this code is automatically generated using Eclipse, it just calls the method in the parent class (super.foo ()) without any change, but in practice we don't use it that way.
After overriding, the overridden function is called directly in the class with method (), but we can still call the (pre-overridden) method in the parent class through super.method ().
6. Create an object
The object is created from the class. Creating an object requires the following three steps:
Declare: declare an object, including the object name and object type.
Instantiation: use the keyword new to create an object.
Initialization: when you create an object using new, the constructor is called to initialize the object.
Public class Dog {public static void main (String [] args) {Dog Dog0 = new Dog ();}} 7, access instance variables and methods
The variable that accesses the instance: the instance name. Variable name
Call the method of the instance: the instance name. Method name ()
Public class Dog {String name; int age; void eat (String food) {System.out.println (name + "is eating" + food + ".");} public Dog () {name = "Dog"; age = 0; System.out.println ("Construction ():"); System.out.println (name + "\ t" + age);} public Dog (String dogName, int dogAge) {name = dogName; age = dogAge System.out.println ("name, age):"); System.out.println (name + "\ t" + age);} public static void main (String [] args) {Dog Dog0 = new Dog (); Dog Dog1 = new Dog ("FooBar", 3); / / access variable Dog0.name = "Ana"; System.out.println (Dog0.name); / / access method Dog1.eat ("cat");}}
Run? The above code will output:
Construction (): Dog 0 construction (name, age): FooBar 3AnaFooBar is eating cat.8, comparison object
The Java language provides two ways to compare objects:
= = operator
Equals () method
8.1 use = = compare objects
= = Syntax compares whether objects are equal, and a = = b returns true only if an and b are equal.
For built-in basic types, their values need to be equal.
For objects, two objects are required to refer to the same object instance.
For example:
Import java.util.logging.Logger;public class Equals {public static void main (String [] args) {Logger l = Logger.getLogger ("Test"); int I = 12, j = 12, k = 99; Integer a = new Integer (12); Integer b = a; Integer c = new Integer (12); l.info ("I = = j:" + (I = j)); l.info ("I = k:" + (I = k)) L.info ("a = = b:" + (a = = b)); l.info ("a = = c:" + (a = = c));}}
Running this code will yield the following results:
I = = j: truei = = k: falsea = = b: truea = = c: false
[note] if you use a = Integer.valueOf (12); and c = Integer.valueOf (12); get an and c respectively, then a = = c will be true!
8.2 compare objects using equals ()
Equals () is a method that is freely available to every Java language object because it is defined as an instance method of java.lang.Object (each Java object inherits this object).
The method to call equals () is as follows:
A.equals (b)
This statement calls the equals () method of object a, passing it a reference to object b.
By default, the Java program uses the = = syntax to check whether two objects are the same. But because equals () is a method, it can be overridden. In this way, we can define the meaning of the appropriate equals () for any object.
[note] when rewriting equals (), we can use object.hashCode () (to return a hash code value for the object.)
Thank you for reading this article carefully. I hope the article "sample Analysis of classes and objects in Java" shared by the editor will be helpful to you. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.