Introduction to JavaSE 11: Java object-oriented and objects

Source: Internet
Author: User

Introduction to JavaSE 11: Java object-oriented and objects

Concepts of Class I and objects

Objects: objects. All objects are objects. All objects that exist objectively are objects. An object is an instance of a class and has statuses and behaviors. For example, a dog is

Objects in the following states: color, name, and type; behavior: tail shaking, calling, and eating.

Class: A class is a template that describes the behavior and status of a class of objects.

2. Objects in Java

Now let's have a deep understanding of what objects are. Looking at the real world around you, you will find many people around you, such as cars, dogs, and people. All these

They all have their own statuses and actions.

Take a dog for example. Its status includes name, breed, color, and behavior: name, tail, and run.

Compared with actual and software objects, they are very similar.

Software objects also have statuses and behaviors. The state of a software object is an attribute, and its behavior is reflected by a method.

In software development, when the internal state of a method operation object changes, the mutual call of objects is also completed through methods.

Classes in Java

Class can be seen as a template for creating Java objects.

The following is a simple class to understand the definition of classes in Java:

 

public class Dog{   String breed;   int age;   String color;   void barking(){       //   }       void hungry(){       //   }       void sleeping(){        //   }}

A class can contain the following types of variables:

A local variable: A variable defined in A method, constructor, or statement block is called A local variable. The variable declaration and initialization are both in the method.

The variable is automatically destroyed.

B Member variable: A member variable is a variable defined in the class and exclusive to the method body. This type of variable is instantiated when an object is created. Member variables can be

Class, constructor, and specific class statement block access.

Class C (static) variables: class variables are also declared in the class, except the method body, but must be declared as static type.

A class can have multiple methods. In the preceding example, barking (), hungry (), and sleeping () are both methods of the Dog class.

Iv. Constructor

Each class has a constructor. If no constructor is explicitly defined for the class, the Java compiler provides a default constructor for the class.

You must call at least one constructor when creating an object. The name of the constructor must be the same as that of the class. A class can have multiple Constructor

Method.

The following is a constructor example:

Public class Puppy {public Puppy () {} public Puppy (String name) {// This constructor has only one parameter: name }}

5. Create objects

The object is created based on the class. In Java, use the keyword "new" to create a new object. To create an object, follow these steps:

A declaration: declares an object, including the Object Name and object type.

B instantiation: Use the keyword new to create an object.

C initialization: when an object is created using new, the constructor is called to initialize the object.

Syntax format: Class Name object name = new Class Name ();

The following is an example of creating an object:

Public class Puppy {public Puppy (String name) {// This constructor has only one parameter: name System. out. println ("Passed Name is:" + name);} public static void main (String [] args) {// The following statement creates a Puppy object Puppy myPuppy = new Puppy ("tommy ");}}

Compile and run the above program and print the following results:

6. Access instance variables and Methods

Access member variables and member methods through created objects, as shown below:

// Instantiate the object ObjectReference = new Constructor (); // access the variable ObjectReference. variableName; // method ObjectReference. MethodName () in the response class ();
Instance

The following example shows how to access instance variables and call Member methods:

Public class Puppy {int puppyAge; public Puppy (String name) {// This constructor has only one parameter: name System. out. println ("Passed Name is:" + name);} public void setAge (int age) {puppyAge = age;} public int getAge () {System. out. println ("Puppy's age is:" + puppyAge); return puppyAge;} public static void main (String [] args) {// create the object Puppy puppy = new Puppy ("tommy"); // you can set the age puppy. setAge (2); // call another method to obtain age puppy. getAge (); // You can also access the member variable System as follows. out. println ("Variable Value:" + puppy. puppyAge );}}

Compile and run the above program to generate the following results:

Seven source file Declaration Rules

Pay special attention to these rules when defining multiple classes in a source file, including import statements and package statements.

A can only have one public class in A source file.

B. A source file may have multiple non-public classes.

The C source file name should be consistent with the Class Name of the public class. For example, if the class name of the public class in the source file is Employee, the source file should

Name it "Employee. java.

D. If a class is defined in a package, the package statement should be in the first line of the source file.

E. If the source file contains the import statement, it should be placed between the package statement and the class definition. If there is no package statement, the import Language

The sentence should be at the beginning of the source file.

The Fimport and package statements are valid for all classes defined in the source file. Different classes cannot be declared for different packages in the same source file.

Class G has several access levels and has different types: abstract classes and final classes.

In addition to the types mentioned above, Java also has some special classes, such as internal classes and anonymous classes.

Eight Java packages

Packages are mainly used to classify classes and interfaces. When developing a Java program, hundreds of classes may be written. Therefore, it is necessary to divide classes and interfaces.

Class.

9. import Statement

In Java, if a complete qualified name, including the package name and class name, the Java compiler can easily locate the source code or

Class. The import Statement is used to provide a reasonable path so that the compiler can find a class.

For example, the following command line will load all classes in the java_installation/java/io path

import java.io.*;

Eleven simple examples

In this example, we create two classes: Employee and EmployeeTest.

Open the text editor and paste the following code. Save the file as Employee. java.

The Employee class has four member variables: name, age, designation, and salary. This class explicitly declares a constructor, which has only one

Parameters.

Import java. io. *; public class Employee {String name; int age; String designation; double salary; // the construction method of the Employee class public Employee (String name) {this. name = name;} // set the value of age public void setAge (int newAge) {age = newAge;} // set the value of public void setDesignation (String newDesig) {designation = newDesig;} // set salary's value to public void setSalary (double newSalary) {salary = newSalary;} // print information to public void printEmployee () {System. out. println ("Name:" + name); System. out. println ("Age:" + age); System. out. println ("Designation:" + designation); System. out. println ("Salary:" + salary );}}

All programs are executed from the main method. To run this program, you must include the main method and create an instance object.

The following describes the EmployeeTest class, which instantiates two instances of the Employee class and calls a method to set the variable value.

Save the following code in the EmployeeTest. java file.

Import java. io. *; public class EmployeeTest {public static void main (String args []) {// use the constructor to create two objects: Employee empOne = new Employee ("James Smith "); employee empTwo = new Employee ("Mary Anne"); // call the member method empOne of the two objects. setAge (26); empOne. setDesignation ("Senior Software Engineer"); empOne. setSalary (1000); empOne. printEmployee (); empTwo. setAge (21); empTwo. setDesignation ("Software Engineer"); empTwo. setSalary (500); empTwo. printEmployee ();}}

Compile these two files and run the EmployeeTest class. The following result is displayed:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.