In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, I would like to share with you the relationship between classes and objects in Java and how to use the relevant knowledge points, the content is detailed, the logic is clear, I believe most people still know too much about this knowledge, so share this article for your reference, I hope you can get something after reading this article, let's take a look at it.
1. Introduce two concepts first.
Class: a class is an abstraction of a class of transactions with common attributes and behaviors in real life, determining the attributes and behaviors that the object will have.
Object: an object is an instance of a class (the object is not looking for a girlfriend), with state and behavior. For example, a dog is an object, its status is: color, name, breed; behavior: wagging tail, barking, eating and so on.
The concept is more abstract, let's talk about a specific project development scenario to understand classes and objects. If we are going to develop a library management system now, how can we express a book in the program? At this point, the concept of class is used, and we can define a data type that represents the book, and this new data type is the class. The above code:
Public class Book {String bookName; / / title String author; / / author Integer price; / / Price}
Now that the class of the book is defined, how do we represent a specific book in the program? For example, using a program to describe the relevant data of the book "one hundred years of Solitude", at this time, we need to create a class (book) entity ("one hundred years of Solitude"), this entity is the object. The above code:
Public class Main {public static void main (String [] args) {Book book = new Book (); book.bookName = "one hundred years of Solitude"; book.author = "M á rquez"; book.price = 55;} 2, composition of the class
Classes are made up of properties and behaviors:
Attributes: represented by member variables in a class
Behavior: represented by member methods in a class
Take the book management system as an example, a book is a class, and the title, author, price and borrowability are all member variables of the class; the behavior of borrowing books is the member method of this class. Let's write another class of books:
Public class Book {/ / member variable String bookName; / / title String author; / / author Integer price; / / Price Boolean state; / / current status / / member method void lendBook () {System.out.println ("borrow one"); System.out.println ("author:" + author "); System.out.println (" Price: "+ price) }} 3. Use of objects
Before using the object, you must first create the object. The keyword for creating the object is new. After creating the object successfully, you can call the member variables and methods in the class.
Public class Main {public static void main (String [] args) {/ / create object Book book = new Book (); / / call class member variable book.bookName = "a hundred years of solitude"; book.author = "M á rquez"; book.price = 55; book.state = true; / / call class method book.lendBook () }} above is all the content of this article entitled "the relationship between classes and objects in Java and how to use it". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
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.