Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction to Java fundamentals: how to use object-oriented and Class Definitions

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains the "introduction to the basis of Java how to use object-oriented and class definition", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn how to use object-oriented and class definition of the introduction to Java!

I. object-oriented

1. Object-oriented is the use of objects in the program to map real things, the relationship between objects to describe the relationship between things.

two。 Object-oriented features:

Encapsulation: encapsulation hides some information of the class inside the class and does not allow external programs to access it. It needs to operate and access the hidden information through the methods provided by this class. Encapsulation is also the core idea of object-oriented, which encapsulates the properties and behavior of the class without giving details to the outside world.

Inheritance: inheritance is a relationship between a class and a class, not an object inheriting. The subclass inherits from the parent class, and the subclass has all the members defined in the parent class, just the inheritance of the class. We create objects, and objects have nothing to do with objects. Satisfying inheritance is a kind of "is a" relationship, and any class that satisfies the "is a" relationship can have an inherited relationship. When Class An is a Class B, then Class A can inherit from Class B. for example, "Cat" inherits "animal", animal is the parent or base class of cat, and cat is a subclass or derivative of animal.

Polymorphism: polymorphism is the multiple forms of an object. The phenomenon of double name can occur in the program, which means that the methods and properties defined in one class are inherited by other classes, they have different data types or show different behaviors, so that the same attribute and method have different meanings in different classes.

II. Definition of class

1. What is a class?

Birds of a feather flock together. Class is a general term for a group of things with the same characteristics or behavior. It is abstract and cannot be used directly. What if you have to use classes? You can only find a specific existence of this kind of thing, and then use this specific existence.

two。 Member variables and member methods can be defined in a class, where member variables are used to describe the characteristics of an object, also known as properties. Member methods are used to describe the behavior of objects, called methods.

3. How to define a class

Class Person {int age;// defines int type variable age / / defines speak () method void speak () {System.out.print ("I am this year" + age+ "year");}}

First, you define a Person class, where Person is the class name, age is the member variable, and speak () member method. The age of the member variable can be accessed in the member method.

4. The variables defined in the class are member variables and the local variables defined in the method. Suppose that defining a local variable in a method is the same as the name of a member variable, then the method accesses the local variable rather than the member variable.

For example:

Class Person {int age=3;// member variable / / define speak () method void speak () {int age=10;// local variable System.out.print ("I am this year" + age+ "year");}} public class p13 {public static void main (String [] args) {/ / TODO Auto-generated method stub Person p1=new Person (); p1.speak ();}}

The result of the output is:

I'm 10 years old.

III. Creation and use of objects

1. What is the object?

The object is a concrete existence in real life. You can see it and touch it. You can use it directly if you bring it over.

two。 Create the format of the object

Class name object name = new class name ()

For example:

Person p=new Person ()

"new Person ()" in the above code is an instance object that creates the Person class, and "Person p" is the variable p of the Person class. "=" means that the address of the Person object in memory is assigned to the variable p. This variable p has a reference to the object.

3. After you create an object, you can access all members of the object through its reference.

Access the format of object members

Object reference. Object member

4. How to access the case of object members

Class Person {int age;// member variable / / define the speak () method void speak () {System.out.println ("my name is Zhang San, this year" + age+ "year");}} public class p13 {public static void main (String [] args) {/ / TODO Auto-generated method stub / / create two Person objects Person p1=new Person (); / / create the first Person object p1.ageaccoun8playimage attribute assignment p1.speak () / / call the object's method Person p2=new Person (); / / create a second Person object p2.speak (); / / call the object's method}}

The result of the output is:

My name is Zhang San, this year 8 years old my name is Zhang San, this year 0 years old thank you for your reading, the above is the "introduction to the basis of Java how to use object-oriented and class definition" of the content, after the study of this article, I believe you on the basic introduction to Java how to use object-oriented and class definition of this problem has a deeper understanding, the specific use of the need for you to practice verification. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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: 207

*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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report