Java Note 1 (2015-8-29)

Source: Internet
Author: User
Tags abstract definition

1, Concept: Class (is to create the template and blueprint of the object, a set of similar objects of the common abstract definition) object (the instantiation of the class result, the real thing). A class is the definition of an object, which cannot be created without a class, and the purpose of a class is to instantiate an object. The class is static and the object is a dynamic instance. The program code operates on the object, and the model gets the class. Objects are composed of methods and properties. A property is a variable.

2. Set a class step:a) define the class name;b) write the class's field to represent its properties;c) Write a method of the class.

3. Creation and use of objects

A) the creation of a class (the definition of a class, the instantiation of a Class) formula:

Data type class name = new data type ();

Data type object Name = new data type ();

Or

Class class Name = new class ();

Supplement: Data type: base data type and reference data type

Reference data types: classes, interfaces, arrays

b) invocation of the class's properties and methods

Called: the member accessor is used. "

Example:

Data type object Name = new data type ();

The name of the object . attributes;

The name of the object . Method name ();

1, Little Red raised two cats, a call small black, this year 2 years old, black,
The other one is called Xiao Bai, 5 years old, white.
Please write a program that asks when the user enters the kitten's name,
It shows the cat's name, age, and color.
If the user entered the kitten name is wrong, it shows "Little Red did not raise the cat." ---------tacit knowledge, the code needs to be simplified. Change the code by 1.

Import Java.util.Scanner;

public class Cat {

public String name;

public int age;

public String color;

Public String toString () {

Return "Name:" +name+ "Age:" +age+ "color:" +color;

}

public static void Main (string[] args) {

TODO auto-generated Method Stub

System.out.println ("Please enter the kitten's name:");

Scanner input=new Scanner (system.in);

String Input_name=input.nextline ();

Cat cat1=new Cat ();

Cat1.name= "Small Black";

cat1.age=5;

Cat1.color= "BLACK";

Cat cat2=new Cat ();

Cat2.name= "Small white";

cat2.age=3;

cat2.color= "White";

if (Input_name.equals (Cat1.name)) {

System.out.println (Cat1.tostring ());

}else if (input_name.equals (Cat2.name)) {

System.out.println (Cat2.tostring ());

Object Invocation Properties: Take the value of the property

System.out.println (Cat2.age);

}else{

System.out.println ("Little Red did not raise such a kitten!") ");

}

}

}

2.

Import Java.util.Scanner;

public class Cat {

String name;

int age;

String color;

Public Cat (String name,int age,string color) {

This.name=name;

This.age=age;

This.color=color;

}

Public String toString () {

Return "Name:" +name+ "Age:" +age+ "color:" +color;

}

public static void Main (string[] args) {

TODO auto-generated Method Stub

Cat blackcat=new Cat ("Small Black", 2, "Black");

Cat whitecat=new Cat ("Small white", 5, "white");

Scanner scan=new Scanner (system.in);

System.out.println ("Please enter the kitten's name:");

String Catname=scan.next ();

System.out.println ("Please enter the age of the Kitten:");

int Catage=scan.nextint ();

if (Catname.equals (blackcat.name) && (catage==blackcat.age)) {

System.out.println (Blackcat.tostring ());

}else if (catname.equals (whitecat.name) && (catage==whitecat.age)) {

System.out.println (Whitecat.tostring ());

}else{

System.out.println ("Little Red does not raise this cat");

}

} }

Java Note 1 (2015-8-29)

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.