Java Basics-The use of custom classes

Source: Internet
Author: User

Custom Classes

We can divide the classes into two types:

1.???????? one is a class that has already been defined in Java, such as the previously used Scanner class,the Random class, which we can just take over.

2.???????? The other is a class that needs to be defined by ourselves, and we can define multiple methods and properties in the class for our actual use.

What is a class? In Java , we can write code in real-life things by description, and we can customize classes to describe things in life. For example, we can describe people, people's names, age, gender are unique attributes, can be in a custom class through variables to describe. People will eat, sleep, learn and other basic functions, we can define these unique functions in a custom class using the method of defining them.

to define the format of a class

public class class name {

???????? To define a property:

The basic characteristics of things can be defined by variables, such as the name of the person :p rivate String name = " Zhang Fei ";

modifier Data Type variable Name = value ;

define the method :

?? used to define the specific function of the thing.

?? modifier return value type Method Name ( parameter list ) {

?

}

}

Here's a piece of code to show how a class is defined:

/* Define a class to describe the phone this thing *///define a mobile phone class Public?class? phone{????????? Define the properties of your phone: brand, color, size, etc.????????? String?brand;????????? String?color;????????? Double?size;}
/* Define the test class for the phone class to test the function in the phone class *///define the test class Public?class? phonetest{????????? Public?static?void?main (String[]?args) {??????????????????? Create an object for the phone class??????????????????? Phone?p?=?new? Phone ();??????????????????? Use the object name to invoke the properties in the phone class and assign the operation??????????????????? P.brand?=? " One plus 3T ";??????????????????? P.color?=? " Star Blue ";??????????????????? p.size?=?5;??????????????????? Get information on your phone??????????????????? SYSTEM.OUT.PRINTLN ("Model:" +p.brand+ ", Color:" +p.color+ ", Size:" +p.size);?????????}}

Operation Result:

Exercises for custom classes

Define the following class, and test:

Rice cooker with properties (brand, capacity size, color, etc.)

Automotive, including attributes (brand, displacement, type, etc.)

Students, including attributes (name, age, gender, etc.)

/* Requirements: Custom definition of Rice cooker class ideas: 1, the definition of rice cooker class, class name Electricbowl??????????? 2, describe the properties of rice cookers, that is, the definition of rice cooker attribute variables??????????? 3, defines the test class, assigns the value to the attribute, and prints out *///to define the rice cooker class Public?class? electricbowl{????????? Define the property variables for the rice cooker????????? String?brand;????????? Double?size;????????? String?color;}

?

/* Requirements: Define car Class Ideas: 1, define car class, class name car??????????? 2, describe the properties of the car, that is, the definition of car property variables??????????? 3, defines the test class, assigns the value to the attribute, and prints out *///defines the automobile class Public?class? car{????????? Describe the car's attribute variables????????? String?brand;????????? Double?displacement;????????? String?type;}

?

/* Define Student class */public?class? student{????????? Defines a student's attribute variable????????? String?name;????????? Int?age;????????? Char?sex;}

?


/* Define test classes, define test classes for three things (rice cookers, cars, students) *///define test class Public?class? test{????????? Public?static?void?main (String[]?args) {??????????????????? Create an object for the rice cooker??????????????????? Electricbowl?eb?=?new? Electricbowl ();??????????????????? Assign a value to the properties of the rice cooker??????????????????? Eb.brand?=? " Beauty ";??????????????????? eb.size?=?1.5;??????????????????? Eb.color?=? " White ";??????????????????? Print the property contents of the rice cooker??????????????????? System.out.println ("Brand:" +eb.brand+ ", Capacity Size:" +eb.size+ ", Color:" +eb.color ");????????????????????????????????????? Create a Car object??????????????????? Car?c?=?new? Car ();??????????????????? Assign a value to the properties of the car??????????????????? C.brand?=? " Chevrolet ";??????????????????? c.displacement?=?2.0;??????????????????? C.type?=? " Section mai ";??????????????????? Print the property contents of the car??????????????????? System.out.println ("Brand: +c.brand+", Displacement: "+c.displacement+", type: "+c.type");????????????????????????????????????? Create student Object??????????????????? Student?s?=?new? Student ();??????????????????? Assign a value to the student's attribute content??????????????????? S.name?=? " Zhang Li ";??????????????????? s.age?=?20;??????????????????? S.sex?=? 'Female ';??????????????????? Print Student's attribute content??????????????????? System.out.println ("Name:" +s.name+ ", Age:" +s.age+ ", Gender:" +s.sex);?????????}}

Operation Result:

ArrayListCollection

the ArrayList collection also belongs to the reference data type, which defines the steps:

1.???????? Import Package:java.util Package

2.???????? Create a variable of reference data type, its definition is a little different

Data type < data type for storing data > variable name = new data type < data type for storing Data > ();

For example, to create a data variable of a string type: arraylist<string> arr = new arraylist<string>;

3.???????? through the variable name . method to invoke it in the same way.

Note: The ArrayList collection stores only reference data types, does not store base data types, and8 base data types have their corresponding 8 reference data types.

ArrayListsome methods in the collection use the

1.Add () adds an element to the collection

2,get () gets the elements in the collection

3,size () Gets the length of the collection

The following code illustrates the use of these methods:

The/*arraylist collection method uses *///to import ArrayList package Import?java.util.arraylist;public?class? arraylistdemo{????????? Public?static?void?main (String[]?args) {??????????????????? Creates a string reference variable for the ArrayList collection??????????????????? Arraylist<string>?array?=?new? Arraylist<string> ();??????????????????? Call the Add method to add a string to the collection??????????????????? Array.add ("Hello");??????????????????? Array.add ("Java");??????????????????? Array.add ("Hello");????????????????????????????????????? Call method gets the length of the collection??????????????????? System.out.println ("The length of the collection is:" +array.size ());??????????????????? The calling method obtains the element content within the collection by index??????????????????? System.out.println (array.get (0));??????????????????? System.out.println (Array.get (1));??????????????????? System.out.println (Array.get (2));?????????}}

Operation Result:

ArrayListCollection Traversal

the traversal of the ArrayList collection is similar to that of the group traversal, which requires the use of a for loop, which shows the traversal of the collection through a piece of code:

/*arraylist Collection of Traversal *///import package Import?java.util.arraylist;public?class? arraylist_1{????????? Public?static?void?main (String[]?args) {??????????????????? Creates a reference variable for an integer collection??????????????????? Arraylist<integer>?array?=?new? Arraylist<integer> ();??????????????????? Adds a value to the collection??????????????????? Array.add (123);??????????????????? Array.add (456);??????????????????? Array.add (789);??????????????????? The data in the collection is obtained through history: there are two methods inside: size (), get ()??????????????????? For (Int?i=0;i<array.size (); i++) {???????????????????????????? System.out.println (Array.get (i));???????????????????}?????????}}
ArrayListsome other methods of the collection use the

Add (int index , element to add )???? ???????? add content to the specified index

Set (int index , modified element content )????? modify content at the specified index

Remove (int index )????????? Delete an element on the specified index

Clear ()?????????????? empties all elements in the collection

The following code shows how these methods are used:

/*arraylist Other methods of collection *///import package Import?java.util.arraylist;public?class? arraylist_2{????????? Public?static?void?main (String[]?args) {??????????????????? Creates a reference type variable for an integer collection??????????????????? Arraylist<integer>?array?=?new? Arraylist<integer> ();??????????????????? Add content to the collection?????????????????? Array.add (+);??????????????????? Array.add (+);??????????????????? Array.add (???????????????????); Adds an element to the specified index??????????????????? Array.add (1,8);??????????????????? Modifies an element at the specified index??????????????????? Array.set (0,6);??????????????????? Deletes the content on the specified index??????????????????? Array.remove (1);??????????????????? Empties all elements in the collection??????????????????? Array.clear ();??????????????????? Iterate through the collection elements and get the elements??????????????????? For (Int?i=0;i<array.size (); i++) {???????????????????????????? System.out.println (Array.get (i));???????????????????}?????????}}


Java Basics-The use of custom classes

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.