0. Object Oriented: OO:
0. OOA (Object Oriented Analysis) OOD (Object Oriented Design) OOP (Object Oriented Programming)
1. It is a software development method
2. Expanded areas : Database systems, interactive interfaces, application platforms, distributed systems, artificial intelligence and other fields
3. It is a method of understanding and abstracting the real world, and is the product of the development of computer programming technology to a certain stage
1. Basic concepts of classes and objects
1. Class: A class is an abstraction of a type of things with similar properties. A class encapsulates the attributes (characteristics) and methods (behavior) of things with similar properties (i.e., members of a class)
2. Object: The object is a specific individual in a class of things
3. Class - object Relationship reference Population - individual
4. The composition of the class: zero or more member variables Zero or more Method Zero or more constructors
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? use using using using ? ? ??? out out out out out out out out out out of out out of ?'s ? ? ? through ‐ ‐ ‐ ‐‐‐‐‐ and
? to use. : Used to construct instances of this class (the constructor will be explained later)
5. Generation and use of objects:
After creation, it can be used. Java objects generally have the following functions: access instances of objects Variables and methods of calling objects
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? use using new keyword to call the constructor of a class to create an instance (object) of this class
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Through using the new keyword’s ’ ’s way out through the constructor
Class name. Class variable | Method
6. General entity class example Dog.java; Declaration and creation of objects; Program entry class (main class), class with main method
7. One person (Zhang San ) was walking on the street and met a dog (Dahuang). The dog (Dahuang) bit the man (Zhang San). The man (Zhang San) beat the dog (Dahuang), and the dog (Dahuang) ran away~!
package Chapter7_2; //Dog類的申明 public class Dog { String name; public void beatPersion(People p){ System.out.println(name+"咬了"+p.name); } public void dogRun(){ System.out.println(name+"跑了......"); } } package Chapter7_2; //People類的申明 public class People { String name; public void walk(){ System.out.println(name+"街上散步"); } public void meetDog(Dog dog){ System.out.println(name+"遇到"+dog.name); } public void beatDog(Dog dog){ System.out.println(name+"打了"+dog.name); } } package Chapter7_2; //主類 public class DogAndPeople { public static void main(String[] args) { People peo=new People(); Dog dog =new Dog(); peo.name="張三"; dog.name="大黃"; peo.walk(); peo.meetDog(dog); dog.beatPersion(peo); peo.beatDog(dog); dog.dogRun(); } }
2. Exercise: Car class, describing the attributes of the car (brand mark, displacement power, color, price)
and methods (start, speedup, normal driving, run, encounter hit (person)) P Write a PeOPLE class with attributes (name name, gender Sex, age Age),
method (driving Drive (car), use the CAR class object as a parameter),
in the main class
1) to let one of them as one). Zhang San drove a BMW speeding down the street
2) Met a person named Li Si
package Chapter7_3; //定義Car類 public class Car { String mark; int power; String color; int price; public void start(People p) { System.out.println(p.name + "啟動了"+mark); } public void speedUp(){ System.out.println(mark+"加速"); } public void run(){ System.out.println(mark+"正常行駛"); } public void hit(People p){ System.out.println(mark+"撞了"+p.name); } } package Chapter7_3; //People類申明 public class People { String name ; char sex; short age; public void dirve(Car car){ System.out.println(name+"駕駛"+car.mark); } } package Chapter7_3; //主類 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="寶馬"; car.power=123; car.price=123143; p1.age=20; p1.name="張三"; p1.sex='m'; p2.age=21; p2.name="李四"; p2.sex='m'; car.start(p1); car.speedUp(); p1.dirve(car); car.run(); car.hit(p2); } }
3. Summary of the use of classes and objects
1. Abstract similar characteristics (attributes) and behaviors (Method) A type of thing is encapsulated into a class
2. Instantiate (new) a specific object according to business needs
3. Initialize and assign values ??to the object in order to obtain the characteristics of the specific object 4. According to business needs Behavior (method) between combined objects
4. Member variables and local variables
1. Member variables are defined in the class, outside the method; also called global variables, the effective scope is the entire class body
2. Local variables are defined in methods, and their scope is inside the method
3. When local variables and member variables have the same name, the member variable is hidden in the method where the local variable is located. If you need to use a 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. About the design of the method
1. Attributes of the method
2. The composition of the method: access modifier return value method name method parameter list
3. Overloading: Method overloading means that a class can have Multiple methods have the same name, but the parameters of these methods must be different, that is, either the number of parameters is different, or the types of parameters are different. ????????
??Exercise: Simple calculator (can add two, three, or four numbers)
?
?
?
? 6. Construction method (constructor)
?
? 1. What is a construction method? What are the characteristics?
2. What is the role of the construction method?
3. When is the constructor called?
Example: 7
Exercise: 8
4. Can the constructor be overloaded?
5. Other instructions:
Constructor is an important way to create objects. A class must contain at least one constructor
程序員如果不給一個類加構造器,則系統(tǒng)會默認添加一個沒有參數(shù)的構造器
7.示例:建立一個學生類,有學號、姓名、年齡、性別屬性,有描述個人信息的方法,通過鍵盤輸入獲取兩個學生的信息,并且打印出描述信息
略
8.構建一個員工信息類(Employee),有員工編號、員工姓名、性別、年齡、工齡、工資等屬性,有工作和休息的方法,還有打印自身信息的方法;
在入口類中通過鍵盤輸入,獲取兩個員工的對象,要求通過帶參數(shù)的構造方法來進行成員變量的初始化。然后根據(jù)輸入星期幾,來輸出員工當前的狀態(tài)(周一至周五工作、周六和周日休息)
package Chapter8_2; //所有類與方法在同一個文件中 import java.util.Scanner; 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 + "今天休息"); } public void work() { System.out.println(name + "今天工作"); } 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) { Scanner scan = new Scanner(System.in); System.out.println("請輸入第一個員工的信息:姓名,工號,性別,年齡,工齡,工資"); String name = scan.next(); int number = scan.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("今天星期幾"); 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("員工信息如下"); System.out.println("姓名"+"\t"+"工號"+"\t"+"性別"+"\t"+"年齡"+"\t"+"工齡"+"\t"+"工資"); sta.printSelf(); } }
9.變量、方法的分類:
?
? ? 內(nèi)存的分配:棧內(nèi)存? 堆內(nèi)存
? ? 創(chuàng)建對象時,通過new關鍵字調(diào)用構造函數(shù),返回當前類的對象:People p=new People();
? ? 對象里有成員變量,會在堆內(nèi)存中開辟連續(xù)的空間存儲成員變量。
? ? p是People類型的引用,p存儲在棧內(nèi)存中,它指向堆內(nèi)存中的對象
?
? ? 這種引用就是C語言中的指針,只是Java語言把這個指針封裝了起來
?
? ? 變量:成員變量? ?局部變量
? ? ? ? 成員變量:類變量(有static關鍵字修飾) 和 實例變量(沒有static關鍵字修飾)
? ? ? ? 局部變量:形參、方法內(nèi)的變量、代碼塊的變量
?
? ? 方法:類方法(有static關鍵字修飾)、實例方法(沒有static關鍵字修飾)
?
? ? java中類的生命周期:加載(Loading)-->驗證(Verification)-->準備(Preparation)-->解析(Resolution)-->
? ? ? ? ? ? ? ? 初始化(Initialization)-->使用(Using)-->卸載(Unloadling)
?
? ? 1、類變量和實例變量的區(qū)別:類變量共享 ,類變量在類加載時分配入內(nèi)存;實例變量每個對象獨有 ,實例變量在類初始化的時候分配內(nèi)存。
? ? 2、類方法和實例方法的區(qū)別:類方法在類加載時分配入口地址;實例方法在類初始化時分配入口地址 (創(chuàng)建第一個對象時)
?
? ? 3、訪問和調(diào)用規(guī)則:
? ? ? ? 1、實例方法既能能訪問實例變量又能訪問類變量
? ? ? ? 2、類方法只能訪問類變量
? ? ? ? 3、實例方法能調(diào)用類方法,但是類方法只能調(diào)用類方法
?
10.對象默認初始化
?
11.總結
? ? 1.類的概念、對象的概念;通過群體和個體的概念理解類和對象的關系
? ? 2.類的成員包括—— 屬性、方法 (有啥,能干啥)
? ? 3.方法的設計
? ? ? ? a.方法名首字母小寫;望文生義;多個單詞組成的方法名首字母小寫,第二個單詞開始首字母大寫
? ? ? ? b.返回類型
? ? ? ? c.方法的重載(參數(shù)的不同,要么參數(shù)個數(shù)不同,要么參數(shù)類型不同)
? ? 4.成員變量和局部變量的區(qū)別;this關鍵字的作用(this相當于對象自身的意思)
? ? 5.關于構造方法
? ? ? ? a.每一個類都有構造方法,不寫不代表沒有(默認無參的構造方法)
? ? ? ? b.構造函數(shù)與new關鍵字息息相關,直接決定了對象的構成方式
? ? ? ? c.帶參數(shù)構造方法的常用方式(給成員變量進行賦值)
? ? 6.實例變量和類變量、實例方法和類方法;關鍵字static
13.作業(yè)
? ? 1.比較大?。簩懸粋€有兩個int型的參數(shù)的方法,要求方法能夠比較這兩個參數(shù)的大小,并且返回比較大的一個,在另外一個類里使用這個方法。
?
? ? 2.判斷 :在一個類里有一個方法,能判斷給定的參數(shù)是否為偶數(shù),如果是偶數(shù)返回true,否則返回false。在另一個類里調(diào)用該方法。
?
? ? 3.寫一個方法,有三個參數(shù),分別為三個線段的長度,判斷三條線段是否能組成一個三角形,如果能返回true,否則返回false。在另一個類中使用
?
? ? 4.寫一個三個參數(shù)的方法,判斷給定的三個參數(shù)能否構成直角三角形。另一個類里使用。

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)