Java04, ubuntu16.04java

Source: Internet
Author: User

Java04, ubuntu16.04java
0. object-oriented (Object Oriented: OO): 0, OOA (Object Oriented Analysis) OOD (Object Oriented Design) OOP (Object Oriented Programming) 1. Software Development Method 2. Extended fields: database systems, interactive interfaces, application platforms, distributed systems, artificial intelligence, and other fields 3. It is a way to understand and Abstract The Real World, is the product of the development of computer programming technology to a certain stage 1. basic concepts of classes and objects 1. class: class is the abstraction of a class of things with similar properties. class encapsulates the attributes (features) and methods (Actions) (members of the class) of similar things. object: an object is a specific entity in a class of things. 3. class-object relationship reference population-individual 4. class composition: zero or multiple member variables zero or multiple methods zero or multiple constructor member variables: defines the State data methods contained by the instance of this class or this class: the constructor used to define the behavior characteristics or function implementation of instances of the class or class: used to construct instances of the class (which will be explained later) 5. object generation and use: You can use it after it is created. J Ava objects have the following functions: the fundamental way to access the instance variables of objects and call object methods to create objects is the constructor, you can use the new keyword to call the constructor of a class to create the instance (object) Object Name of the class. instance variable | method class name. class variable | Method 6. general entity class example Dog. java; object declaration and creation; program entry class (main class), Class with main method 7. A man (Zhang San) walked in the street and met a dog (rhubarb). A dog (rhubarb) bit a man (Zhang San) and a man (Zhang San) beat a dog (rhubarb ), the dog (rhubarb) ran ~! Package Chapter7_2; // Declaration of the Dog class public class Dog {String name; public void beatPersion (People p) {System. out. println (name + "bit" + p. name);} public void dogRun () {System. out. println (name + "ran ...... ") ;}} package Chapter7_2; // Declaration of the People class public class People {String name; public void walk () {System. out. println (name + "walking on the street");} public void meetDog (Dog dog) {System. out. println (name + "encounter" + dog. name);} public voi D beatDog (Dog dog) {System. out. println (name + "hit" + dog. name) ;}} package Chapter7_2; // main class public class DogAndPeople {public static void main (String [] args) {People peo = new People (); dog dog = new Dog (); peo. name = "James"; dog. name = "rhubarb"; peo. walk (); peo. meetDog (dog); dog. beatPersion (peo); peo. beatDog (dog); dog. dogRun () ;}} 2. exercise: Car class, describes the attributes of a Car (brand mark, displacement power, color, price) and methods (start, speed up, run, touch To hit (person), write a People class with attributes (name, gender sex, age), method (Driving drive (car), with the Car class object as the parameter ), in the main category 1) let a person drive a BMW Car on the street 2) met a person called Li Si package Chapter7_3; // defines the Car class public class Car {String mark; int power; String color; int price; public void start (People p) {System. out. println (p. name + "started" + mark);} public void speedUp () {System. out. println (mark + "acceleration");} public void run () {System. out. println (mark + "normal");} public v Oid hit (People p) {System. out. println (mark + "hit" + p. name) ;}} package Chapter7_3; // The People class declares public class People {String name; char sex; short age; public void dirve (Car car) {System. out. println (name + "Driving" + car. mark) ;}} package Chapter7_3; // main class public class CarAndPerple {public static void main (String [] args) {Car car = new Car (); people p1 = new People (); People p2 = new People (); car. color = "black"; car. Mark = "BMW"; car. power = 123; car. price = 123143; p1.age = 20; p1.name = "Zhang San"; p1.sex = 'M'; p2.age = 21; p2.name = "Li Si"; p2.sex = 'M'; car. start (p1); car. speedUp (); p1.dirve (car); car. run (); car. hit (p2) ;}} 3. class and object usage Summary 1. abstract A class of things with similar features (attributes) and behaviors (methods) are encapsulated in the class 2. instantiate (new) an object according to business needs 3. initialize and assign values to objects to obtain the features of objects. combine actions (methods) between objects based on business needs 4. member variables and local variables 1. the member variables are defined in the class, outside the method, and also known as global variables. The valid scope is the entire class body. 2. local variables are defined in the method, and the scope is within the method. 3. when the local variables and member variables have the same name The method where the local variable is located is hidden. If you need to use the hidden global (member) variable in this method, you need to use the this keyword this: if the local variable and the member variable have the same name, you can call the member variable in the code block where the local variable is located. 5. design of methods 1. attribute of method 2. method composition: access modifier return value method name method parameter list 3. overload: A method overload means that multiple methods in a class can have the same name, but the parameters of these methods must be different, that is, the number of parameters is different, or the parameter type is different. Exercise: simple calculator (two, three, and four numbers can be added) 6. constructor 1. What is constructor? What are the features? 2. What is the role of the constructor? 3. When will the constructor be called? Example: 7 exercise: 8 4. Can the constructor be reloaded? 5. other Instructions: constructor is an important way to create objects. A Class must contain at least one constructor. If a programmer does not add constructor to a class, A constructor without parameters is added by default. example: Create a student class that includes student ID, name, age, and Gender attributes and describes personal information. Enter the information of the two students on the keyboard and print the description information. construct an Employee information class, including Employee ID, Employee name, gender, age, length of service, salary, and other attributes, work and rest methods, and print Employee information; in the entry class, input the keyboard to obtain the objects of two employees. The initialization of member variables must be carried out through the construction method with parameters. Then, based on the day of the week, output the current state of the employee (working from Monday to Friday, resting on Saturday and Sunday) package Chapter8_2; // all classes and methods are imported to java in the same file. util. secret; public class Staff {int number; String name; char sex; short age; short w_age; long mon; public Staff (String name, int number, char sex, short age, short w_age, long mon) {this. name = name; this. number = number; this. sex = sex; this. age = age; this. w_age = w_age; this. mon = mon;} public void rest () {System. out. println (name + "Today's break");} public void work () {System. out. println (name + "work today");} public void printSelf () {System. out. println (name + "\ t" + number + "\ t" + sex + "\ t" + age + "\ t" + w_age + "\ t" + mon +" \ t ");} public static void main (String [] args) {custom scan = new custom (System. in); System. out. println ("enter the information of the first employee: name, employee ID, gender, age, length of service, salary"); String name = scan. next (); int number = sca N. nextInt (); char sex = scan. next (). charAt (0); short age = scan. nextShort (); short w_age = scan. nextShort (); long mon = scan. nextLong (); Staff sta = new Staff (name, number, sex, age, w_age, mon); System. out. println ("the day of the week"); short day = scan. nextShort (); if (day <6) {sta. work ();} else if (day >=6 & day <= 7) {sta. rest ();} else {System. out. println ("error! ");} System. out. println ("employee information as follows"); System. out. println ("name" + "\ t" + "employee ID" + "\ t" + "gender" + "\ t" + "Age" + "\ t" + "Length of Service "+" \ t "+" salary "); sta. printSelf () ;}} 9. classification of variables and Methods: Memory Allocation: when an object is created in the stack memory heap, the constructor is called using the new keyword to return the object of the current class: People p = new People (); the object contains member variables, which will open up continuous space storage member variables in the heap memory. P is a reference of the People type. p is stored in the stack memory. It points to objects in the heap memory. This reference is a pointer in C language, but the Java language encapsulates the pointer as a variable: member variables local variables member variables: class variables (modified with the static keyword) and instance variables (not modified with the static keyword) local variables: form parameters, variables in methods, variable methods of code blocks: class Method (with static keyword modifier), instance method (without static keyword modifier) java class lifecycle: Loading --> Verification --> Preparation) --> Resolution --> Initialization --> Using --> Unloadling 1. Differences between class variables and instance variables: class variables are shared, class variables are allocated to the memory when the class is loaded. Each instance variable is unique to each object. instance variables are allocated to the memory when the class is initialized. 2. Differences between the class method and the instance method: the class method allocates the entry address when the class is loaded; the instance method assigns the entry address when the class is initialized (when the first object is created) 3. Access and call rules: 1. the instance method can both access instance variables and invoke class variables. 2. The class method can only access class variables. 3. The instance method can call class methods, however, class methods can only call class Methods 10. object initialization 11 by default. conclusion 1. concepts of classes and objects; understanding the relationship between classes and objects through concepts of groups and individuals 2. class Members include attributes and methods (What are there and what can be done). 3. method Design. the method name starts with B. return type c. method overload (different parameters, or different number of parameters, or different parameter types) 4. differences between member variables and local variables; the role of this keyword (this is equivalent to the meaning of the object itself) 5. constructor. every class has a constructor. If it is not written, it does not mean that there is no (no constructor by default) B. constructor is closely related to the new keyword, Directly determines the object composition method c. common methods with parameter Constructor (assign values to member variables) 6. instance variables and class variables, instance methods, and class methods; keyword static13. Job 1. compare size: Write a method with two int parameters. The method must be able to compare the values of the two parameters and return a larger one. Use this method in another class. 2. Judgment: there is a method in a class to determine whether the given parameter is an even number. If it is an even number, true is returned; otherwise, false is returned. Call this method in another class. 3. Write a method with three parameters, which are the length of three line segments, and determine whether three line segments can form a triangle. If true is returned, otherwise false is returned. In another class, use the 4. Write a three-parameter method to determine whether the given three parameters can constitute a right triangle. In another class.

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.