面向对象
面向对象概述
面向对象是一种思想
对象
概念:万物皆对象 现实生活中 所有的事物都是对象
京东 人 酸奶
对象有什么:对象的属性
对象能做什么:对象的行为
面向对象的思想
把大象放冰箱 总共分几步
把冰箱门打开
把大象放进去
把冰箱门关上
冰箱.门打开
冰箱.存东西(大象)
冰箱.关门
面向过程的思想:自顶向下 逐步执行
面向对象思想:以对象为中心,程序的使用者,调用对象的行为 而不去关心行为是怎么实现的
类
概念:所有具有相同特征和行为的对象的共性抽取抽象出来的一个概念 这个概念叫做类
对象一定具有类中描述的属性和行为
类是创建对象的模版
一类事物的属性 :实例变量(成员变量)
行为:实例方法(成员方法)
创建一个类
关键词:class
语法:class 类名{
}
创建对象
语法:
类名 对象名 = new 类名();
案例:
public class Demo{
public static void main(String[] args) {
老婆 小丽 = new 老婆();
小丽.name = "范冰冰";
小丽.sex = "女";
小丽.age = 18;
System.out.println(小丽.sex);
System.out.println(小丽.age);
System.out.println(小丽.name);
}
}
class 老婆{
String name;
int age;
String sex;
}
实例变量(成员变量)
定义位置:类中 方法外
语法:数据类型 变量名;
通过对象访问属性:对象名.属性名
作用域:至少在本类
成员变量和成员变量不可以重名
局部变量和局部变量存在重合作用域时 不可以重名
成员变量和局部变量可以重名,但是局部变量优先
默认值:和数组是一样的
整型 0
浮点型 0.0
字符类型: '\u0000'
boolean:false
引用数据类型: null
注意:实例变量的赋值也可以直接在类中进行初始化
案例:
public class Demo{
public static void main(String[] args) {
老婆 小丽 = new 老婆();
小丽.name = "范冰冰";
小丽.sex = "女";
小丽.age = 18;
System.out.println(小丽.sex);
System.out.println(小丽.age);
System.out.println(小丽.name);
}
}
class 老婆{
String name = "林志玲";
int age = 17;
String sex = "女";
}
案例:
public class Demo{
public static void main(String[] args) {
Student s1 = new Student();
s1.speakAge();
}
}
class Student{
String name ;
int age = 18;
int age = 98;
String sex;
double score;
public void study() {
System.out.println("学Java 能致富 能娶媳妇 ");
}
public void speakAge() {
int age = 98;
System.out.println("我的年龄是:"+age);
}
}
成员变量和局部变量的比较
| 成员变量 | 局部变量 |
---|
位置 | 类中 方法外 | 方法中 |
作用域 | 至少本类 | 从定义行开始到所在代码块结束 |
默认值 | 有默认值 | 没有默认值 |
重名 | 成成之间不可 但是成局之间可以 局部变量优先 | 局局之间存在重合作用域不可重名 |
成员方法
定义位置:类中
定义语法:将之前函数中的static去掉
修饰符 返回值类型 方法名(形参列表){
方法的实现;
}
案例:
public 返回值类型 方法名(形参列表){
}
代码:
public class Demo{
public static void main(String[] args) {
Student s1 = new Student();
s1.age = 88;
s1.speakAge();
}
}
class Student{
String name ;
int age = 18;
String sex;
double score;
public void study() {
System.out.println("学Java 能致富 能娶媳妇 ");
}
public void speakAge() {
System.out.println("我的年龄是:"+age);
}
}
方法的重载(OverLoad)
案例:定义一个Person类
public class Demo{
public static void main(String[] args) {
Person p1 = new Person();
p1.eat(口香糖);
}
}
class Person{
String name;
int age;
public void eat(粥 a) {}
public void eat(口香糖 b) {}
public void eat(馒头 c) {}
}
=========================================================================================
概念:定义同名的多个方法,方法的实现不同 参数列表不同 在调用的时候系统会根据所传的参数自动匹配所对应的方法,用来屏蔽参数类型不同,方法实现不同的做法叫做方法的重载。
特点:
1.方法名相同
2.参数列表不同(类型 个数 顺序)
3.和修饰符 返回值 无关
案例:
public class Demo{
public static void main(String[] args) {
MyMath m1 = new MyMath();
int a = 34;
int b = 98;
int sum = m1.sum(a,b);
System.out.println(sum);
}
}
class MyMath{
public int sum(int a,int b) {
System.out.println("两个整数的和");
int sum = a+b;
return sum;
}
public double sum(double a,double b) {
System.out.println("两个小数的和");
return a+b;
}
public int sum(int a,int b,int c) {
System.out.println("三个整数的和");
return a+b+c;
}
}
=========================================================================================
注意:参数名不同 不能叫做重载
案例:
public class Demo{
public static void main(String[] args) {
MyMath m1 = new MyMath();
int a = 34;
int b = 98;
int sum = m1.sum(a,b);
System.out.println(sum);
}
}
class MyMath{
public int sum(int a,int b) {
System.out.println("两个整数的和");
int sum = a+b;
return sum;
}
public double sum(double a,double b) {
System.out.println("两个小数的和");
return a+b;
}
public int sum(int a,int b,int c) {
System.out.println("三个整数的和");
return a+b+c;
}
}
=======================================================================================
案例:以下两个方法构成重载 因为 参数列表的顺序不同
public class Demo{
public static void main(String[] args) {
MyMath m1 = new MyMath();
int a = 34;
int b = 98;
int sum = m1.sum(a,b);
System.out.println(sum);
}
}
class MyMath{
public int sum(int a,int b) {
System.out.println("两个整数的和");
int sum = a+b;
return sum;
}
public double sum(double a,double b) {
System.out.println("两个小数的和");
return a+b;
}
public int sum(int a,int b,int c) {
System.out.println("三个整数的和");
return a+b+c;
}
public double sum(int a,double b) {
return a+b;
}
public double sum(double a,int b) {
return a+b;
}
}
========================================================================================
案例:当调用方法时,传入的参数类型和类中定义的方法没有匹配,会向上就近匹配方法
public class Demo{
public static void main(String[] args) {
MyMath m1 = new MyMath();
int a = 34;
int b = 98;
double sum = m1.sum(a,b);
System.out.println(sum);
}
}
class MyMath{
public double sum(double a,double b) {
System.out.println("两个小数的和");
return a+b;
}
}
=========================================================================================
案例:在调用方法时避免调用不明确
public class Demo{
public static void main(String[] args) {
MyMath m1 = new MyMath();
int a = 34;
int b = 98;
double sum = m1.sum(a,b);
System.out.println(sum);
}
}
class MyMath{
public double sum(double a,double b) {
System.out.println("double double");
return a+b;
}
public double sum(int a,double b) {
System.out.println("int double");
return a+b;
}
public double sum(double a,int b) {
System.out.println("double int");
return a+b;
}
}
构造方法
特点:
1.构造方法的方法名和类名一致(包括大小写)
2.构造方法没有返回值类型(连void都没有)
3.构造方法可以重载
4.构造方法不可以手动调用,只能在创建对象的时,jvm自动调用
5.构造方法在创建对象时只能调用一次
构造方法的作用:
1.可以为属性进行赋值
2.用于创建对象
注意:当一个类中,没有定义构造方法 系统会自动提供一个公开的 无参的构造方法 当类中已经定义了构造方法,系统不再提供无参公开构造,如果需要使用无参的构造 那么必须自己定义出来 一般开发如果定义了有参的构造 都会再定义一个无参的构造
案例:
public class Demo{
public static void main(String[] args) {
Student s = new Student();
s.Student();
}
}
class Student{
public Student(){
}
String name;
int age;
public void study(){
System.out.println("学习Java");
}
}
=========================================================================================
案例:关于系统提供的公开无参构造问题
public class Demo{
public static void main(String[] args) {
Student s = new Student();
System.out.println(s.name);
System.out.println(s.age);
}
}
class Student{
public Student(String a,int b){
System.out.println("我是有参构造");
name = a;
age = b;
}
String name;
int age;
public void study(){
System.out.println("学习Java");
}
}
对象创建的步骤
1.为对象分配空间并为属性赋默认值
2.为属性进行初始化
3.调用构造方法 并为属性再次赋值
具体步骤见图示