Rediscover Java Memoirs (ii): JAVA02 Classes and objects

Source: Internet
Author: User


Class: Subjective abstraction, the template of an object, can be instantiated as an object---Define a template, and the template cannot be used directly

--- use a class to create an object

--The actual objects in the real, abstract results (emphasizing the same point between objects, ignoring the different processes)

-- Use abstract results: Instantiate (instantiate) a template and manipulate it with instantiated results

Habitually define the format of a class

Package XXX;

Import XXX;

[Modifier] Class Name {

Properties: Represented by a variable (type of basic data)

Behavior: Use method Representation (method of basic statement)

Constructor method (the method used when creating the class, the primary function is to give the initial value to the attributes in the class)

}

--Class, object, instance 3 relationships among the people

Class: The template of an object, instantiated as an object (a template that makes up space)

Object: The individual of the class (the referenced pointer)

Instance: implemented object (memory space)

Student s=new Student ();

Homework:

1. Generate a random integer between 100 and 0-999, and then judge the 100 random

Integers which are primes and which are not?

Program Analysis: Primes are integers that can be divisible only by 1 and itself. Determine a

Integer n is a prime number to determine whether an integer n can be removed by 1 and its own

If all integers are not divisible, then n is the prime number.

Program Design I can start from 2, to the integer n of 1/2, using I sequentially

Remove the integer that needs to be judged, as long as there is a case where the number can be divisible

Determines whether the integer to be judged is not a prime number, otherwise it is a prime number.

"Program 2"

2. Print an n-line pyramid pattern on the screen, for example, if n=5, the pattern is as follows:

*

***

*****

*******

*********

3. The elements in an array are inverted and cannot be opened with a new array of pro

Storage space, can only be changed on the original array.

4. Create an array class with an array initialization size of 10 elements. When the elements in the array are filled, the array class

Capacity automatic expansion of 10. Write a test class to test the above program!

5. (1) Write a class named animal, which has two private properties, name

(representing the name of the animal), and legs (the number of bars representing the animal's legs);

Requires a public access method for two private properties. and provide

Two overloaded construction methods, one requires two parameters, respectively, with this

Two parameters give the private property name and legs assignment. Another without a parameter,

By default, the name is assigned a value of AAA, assigning a value of 4 to the legs, and requiring a second

The first constructor method is called in a constructor method. The class also has two heavy

The move () method, one of which has no parameters, to output a line on the screen

Text: xxxmoving!! (XXX is the name of the animal);

to an int parameter n, output XXX moving n meters on the screen!!

(2) write a kind of fish, inherit from the animal class, and provide a constructor method,

The constructor requires a parameter name and assigns the default value of 0 to legs;

The animal class also requires that the non-parametric move () method in the class be overwritten, requiring the output

: xxxswimming!!

(3) Write a class bird, inherit from the animal class, and provide a constructor method,

The constructor requires a parameter name and assigns the default value of 2 to legs;

The animal class also requires that the non-parametric move () method in the class be overwritten, requiring the output

: xxxflying!!

(4) Write a class zoo, define a Main method, in the main method to generate if

Do a animal,fish and bird. and call their properties and methods.

6. Write a class person that contains the following properties: String name; int age;

Boolean gender; Person Partner (spouse). Write one for the person class

Marry (Person p) method, which represents the current object and p to marry,

Marriage, then output congratulations, otherwise the output can not marry reason. Requirements in another

Class and write a master method to test the above program.

You cannot marry under the following circumstances:

1, same-sex;

2, not reaching the age of marriage, male <24, female <22;

3, one party is married



Rediscover Java Memoirs (ii): JAVA02 Classes and objects

Related Article

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.