Java04

高洛峰
Release: 2016-10-10 09:00:09
Original
2192 people have browsed it

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(); } }
Copy after login

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); } }
Copy after login

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

程序员如果不给一个类加构造器,则系统会默认添加一个没有参数的构造器

7.示例:建立一个学生类,有学号、姓名、年龄、性别属性,有描述个人信息的方法,通过键盘输入获取两个学生的信息,并且打印出描述信息

Copy after login

8.构建一个员工信息类(Employee),有员工编号、员工姓名、性别、年龄、工龄、工资等属性,有工作和休息的方法,还有打印自身信息的方法;

在入口类中通过键盘输入,获取两个员工的对象,要求通过带参数的构造方法来进行成员变量的初始化。然后根据输入星期几,来输出员工当前的状态(周一至周五工作、周六和周日休息)

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(); } }
Copy after login

9.变量、方法的分类:

内存的分配:栈内存 堆内存

创建对象时,通过new关键字调用构造函数,返回当前类的对象:People p=new People();

对象里有成员变量,会在堆内存中开辟连续的空间存储成员变量。

p是People类型的引用,p存储在栈内存中,它指向堆内存中的对象

这种引用就是C语言中的指针,只是Java语言把这个指针封装了起来

变量:成员变量 局部变量

成员变量:类变量(有static关键字修饰) 和 实例变量(没有static关键字修饰)

局部变量:形参、方法内的变量、代码块的变量

方法:类方法(有static关键字修饰)、实例方法(没有static关键字修饰)

java中类的生命周期:加载(Loading)-->验证(Verification)-->准备(Preparation)-->解析(Resolution)-->

初始化(Initialization)-->使用(Using)-->卸载(Unloadling)

1、类变量和实例变量的区别:类变量共享 ,类变量在类加载时分配入内存;实例变量每个对象独有 ,实例变量在类初始化的时候分配内存。

2、类方法和实例方法的区别:类方法在类加载时分配入口地址;实例方法在类初始化时分配入口地址 (创建第一个对象时)

3、访问和调用规则:

1、实例方法既能能访问实例变量又能访问类变量

2、类方法只能访问类变量

3、实例方法能调用类方法,但是类方法只能调用类方法

10.对象默认初始化

11.总结

1.类的概念、对象的概念;通过群体和个体的概念理解类和对象的关系

2.类的成员包括—— 属性、方法 (有啥,能干啥)

3.方法的设计

a.方法名首字母小写;望文生义;多个单词组成的方法名首字母小写,第二个单词开始首字母大写

b.返回类型

c.方法的重载(参数的不同,要么参数个数不同,要么参数类型不同)

4.成员变量和局部变量的区别;this关键字的作用(this相当于对象自身的意思)

5.关于构造方法

a.每一个类都有构造方法,不写不代表没有(默认无参的构造方法)

b.构造函数与new关键字息息相关,直接决定了对象的构成方式

c.带参数构造方法的常用方式(给成员变量进行赋值)

6.实例变量和类变量、实例方法和类方法;关键字static

13.作业

1.比较大小:写一个有两个int型的参数的方法,要求方法能够比较这两个参数的大小,并且返回比较大的一个,在另外一个类里使用这个方法。

2.判断 :在一个类里有一个方法,能判断给定的参数是否为偶数,如果是偶数返回true,否则返回false。在另一个类里调用该方法。

3.写一个方法,有三个参数,分别为三个线段的长度,判断三条线段是否能组成一个三角形,如果能返回true,否则返回false。在另一个类中使用

4.写一个三个参数的方法,判断给定的三个参数能否构成直角三角形。另一个类里使用。


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!