Java Learning Note 2015-6-6

Source: Internet
Author: User

Classes and Objects (Concept demo demo)

Object-oriented Java C + + encapsulation inherits polymorphic class objects

Process-oriented C-structured body


1. Get user input 2, logic judgment 3, operation output result


Process-oriented C structure content: pointer structure Body header file


Class is the foundation of an object-oriented programming language


Class? A human computer as long as it is a group of common things (with common characteristics and movements) gathered together


Collections/Templates


Description is a description of the entire class. (Description of common action common characteristics is not responsible for the implementation of specific reference)


Object constructs a specific reference from a collection/template (assigns/describes some specific values or actions)


Call via object (Static)


Association


Attribute build Justice with private
Methods recommend public


Homework:


Delmar and Ice Battle PK game turn system


Each turn Delmar and ice attack each other onceDelmar attacked the ice once, ice attacked Delmar once.


Keep hitting until one side has less than 0 blood!


Summarize:
Class describes several classes an execution class


Describe Class Property methods


Execution class: Main function call code business Process Control


He's a bridge between functions and the outside world. This class and the outer class


Next Lesson Constructor this




Demand analysis: XX car on the road road status see forward and backward parking


According to the requirements of the name brand model car Road state distance mileage


1, which nouns can be categorized into a category to not return to 2, which nouns and what are the nouns


Car: Brand model State Road: Nouns are either classes or attributes or superfluous


Pull away the verb to move forward back parking verb must be a method of a class (function)




Car: Drive forward back stop




Create a class: Car cars class


Market research-"Demand analysis-" Summary design-"Detailed design-" Code design-"Code preparation-" test-"demo-> online


Car Category: Brand model status


Only attribute methods that can be written directly in a class's code snippet


public class car{


Property (is a global variable) the name of the property is also as meaningful as possible
Because the attribute is a noun that describes the characteristics of the class


String brand; Naming of brand attributes and naming conventions for variables all lowercase
String model; Model Qualifier Data Type property name (parsed)
String Statu;


int length; Mileage
//***********************************************************


method is a verb that describes the action of the class


* Qualifier * Return value type method name (formal parameter list) {code snippet}


public void Drive () {

length++;


System.out.print (brand+ "brand" +model+ "model of the car status is:" +statu+ "Status! ");
}


public void ahead () {


length++;

System.out.print (brand+ "brand" +model+ "model of the car status is:" +statu+ "Status! ");


}


public void back () {


length--;


System.out.print (brand+ "brand" +model+ "model of the car status is:" +statu+ "Status! ");


}


public void Stop () {


System.out.print (brand+ "brand" +model+ "model of the car status is:" +statu+ "Status! ");


}


}


/* 2nd lesson Assignment for Education of choice
* Classes and objects
* Ice and Delmar Battle game Dema class implementation
* Instructor Bear
* Student Cao Kun
* 2015-6-6
*/


/* Demand Analysis


* Delmar and Ice Battle PK game turn system


* Each turn Delmar and ice attack each other onceDelmar attacked the ice once, ice attacked Delmar once.


* Keep hitting the side of a blood volume below 0!

*/


public class dema{


Defining properties and methods for a class
Private String dname;
private int dblood; Health value
private int dattackpower; Damage inflicted on the other's health at a time of attack.




public void Setdname (String dname) {

This.dname = dname;

}


Public String Getdname () {

return this.dname;


}


public void Setdblood (int dblood) {

This.dblood = Dblood;

}


public int Getdblood () {

return this.dblood;


}


public void Setdattackpower (int dattackpower) {

This.dattackpower = Dattackpower;


}


public int Getdattackpower () {

return this.dattackpower;


}


public void Dattack (hanbing hanbing) {

Hanbing.sethblood (Hanbing.gethblood ()-this.getdattackpower ());

System.out.println ("The name called:" +this.getdname () + "Delmar Warrior gives the name called:" +hangbing.gethname () + "ice Warrior, a pounding attack, which causes the ice warrior" + Hanbing.gethname () + "Loss of Health:" +this.getdattackpower () + ", Ice Warrior:" +dema.gethname () + "remaining health:" +hbpower);


}


}


/* 2nd lesson Assignment for Education of choice
* Classes and objects
* Ice and Delmar Battle game Hanbing class implementation
* Instructor Bear
* Student Cao Kun
* 2015-6-6
*/


/* Demand Analysis


* Delmar and Ice Battle PK game turn system


* Each turn Delmar and ice attack each other onceDelmar attacked the ice once, ice attacked Delmar once.


* Keep hitting the side of a blood volume below 0!

*/


public class hanbing{


Defining properties and methods for a class
Private String Hname;
private int hblood; Health value
private int hattackpower; Damage inflicted on the other's health at a time of attack.




public void Sethname (String hname) {

This.hname = Hname;

}


Public String Gethname () {

return this.hname;


}


public void Sethblood (int hblood) {

This.hblood = Hblood;

}


public int Gethblood () {

return this.hblood;

}


public void Sethattackpower (int hattackpower) {

This.hattackpower = Hattackpower;


}


public int Gethattackpower () {

return this.hattackpower;


}


public void Hattack (Dema Dema) {

Dema.setdblood (Dema.getdblood ()-this.gethattackpower ());

System.out.println ("The name is called:" +this.gethname () + "The Ice Warrior gives the name called:" +dema.getdname () + "The Lockheed Warrior a pounding attack, which caused the German Mars Warrior" +dema.getdname () + "Loss of Health": "+this.gethattackpower () +", Delmar Warrior: "+dema.getdname () +" remaining health: "+demapower);


}


}



/* 2nd lesson Assignment for Education of choice
* Classes and objects
* Ice and Delmar the implementation of the game execution class
* Instructor Bear
* Student Cao Kun
* 2015-6-6
*/


/* Demand Analysis


* Delmar and Ice Battle PK game turn system


* Each turn Delmar and ice attack each other onceDelmar attacked the ice once, ice attacked Delmar once.


* Keep hitting the side of a blood volume below 0!

*/


public class manager{


public static void Main (String args[]) {


hanbing HB = new hanbing ();
Dema dm = new Dema ();

Hb.sethname ("Zhang San");
Hb.sethblood (1000);
Hb.sethattackpower (95);


Dm.setdname ("John Doe");
Dm.setdblood (900);
Dm.setdattackpower (108);


while (true) {


Hb. Hattack (DM);


System.out.println ("Ice Warrior:" +hb.gethname () + "given to the Lockheed Warrior:" +dm.getdname () + "one attack, resulting in a de Mars warrior:" +dm.getdname () + "Loss of Health:" + Hb.gethattackpower () + ", Delmar Warrior:" +dm.getdname () + "remaining Health:" +dm.getdblood ());

if (Hb.gethblood () <=0 | | dm.getdblood () <=0)

Break


Dm. Dattack (HB);

System.out.println ("De mars Warrior:" +dm.getdname () + "Give the Ice Warrior:" +hb.gethname () + "one attack, causing the Ice Warrior:" +hb.gethname () + "Loss of Health:" + Dm.getdattackpower () + ", Ice Warrior:" +hb.gethname () + "remaining Health:" +hb.gethblood ());

if (Hb.gethblood () <=0 | | dm.getdblood () <=0)

Break
}


if (Hb.gethblood () <=0) {

SYSTEM.OUT.PRINTLN ("The Lockheed Warrior:" +dm.getdname () + "Victory! ");


}else{

System.out.println ("Ice Warrior:" +hb.gethname () + "Victory! ");
}





}
}

Java Learning Note 2015-6-6

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.