一、面向对象的思想

如何将大象装进冰箱?

(1)、面向过程思想:强调的动作、行为的过程

第一步:将冰箱门打开

第二步:将大象放进冰箱

第三步:将冰箱门关上

(2)、面向对象的过程:强调的是对象的本身

例如上例用面向对象的思想先找到冰箱这个实体,那么冰箱本身具有打开、存储和关闭的功能

使用面向对象的好处:

(1)符合人们的思维习惯

(2)将复杂问题简单化

(3)面向对象让曾经在过程中的执行者,变成了对象中的指挥者

Java的思想:一切皆为对象

面向对象的三大特征:

封装 (Encapsulation)

继承 (Inheritance)

多态 (Polymorphism)

面向对象举例:如果将对象比作汽车,那么类就是汽车的设计图纸。所以面向对象程序设计的重点是类的设计,而不是对象的设计

使用java面向对象编程 java面向对象简单例子_System


日常生活中的对象:

使用java面向对象编程 java面向对象简单例子_Computer_02


类和对象的关系:类是抽象的,对象是具体的。生活案例:

类是一个图纸 对象是根据该图纸制造多个实物

类是一个模具 对象是使用模具制造的多个铸件

类是汽车,对象就是购买的一辆辆具体的桑塔纳汽车

怎么描述对象?

(1)找到该对象所属类别

(2)找到该对象的特征(属性)

(3)找到对象有那些行为

Java定义类的语法格式:

public class 类名

{

//定义属性部分

属性1的类型 属性1;

属性2的类型 属性2;


属性n的类型 属性n;

//定义方法部分
           方法1;
           方法2;
                 …
           方法m;

}
Java定义行为方法的语法为:
<修饰符> <返回类型> <方法名>([< 参数表>]) {
[< 语句>]
}
说明: 修饰符:public,private,protected 等; 返回类型:return语句传递返回值。没有返回值:void
第一个面向对象的程序:
(1)所属类别 GirlFriend
(2)定义属性
和以前定义变量一样
(3)定义行为

行为调用的语法:
对象名.方法名();

属性调用的语法:
对象名.属性

class GirlFriend
 {
 String sex;
 int age;
 float height;
 public void work()
 {
 System.out.println(“我的女朋友能工作!”);
 }
 public void study()
 {
 System.out.println(“我的女朋友学习好!”);
 }
 }
 public class Test001
 {
public static void main(String[] args) 
{
	GirlFriend lily = new GirlFriend();
	lily.work();
	lily.sex="F";
	lily.age=20;
	lily.height=166;
	System.out.println("女友年龄:"+lily.age);
}

}
注意:一个源文件里面只能有一个带public的类
再例如:

class NotePad{
 public String type=“HP-001”;
 public void computing()
 {
 int count=0;
 for(int i=100;i<=999;i++)
 {
 int b=i/100;
 int s=i%100/10;
 int g=i%10;
 if(i==bbb+sss+ggg)
 {
 System.out.println(“水仙花是:”+i);
 count++;
 }
 }
 System.out.println(“水仙花的个数是:”+count+“个”);
 }}

注意问题:
1、主类的名称必须和源文件名称保持一致
2、只有主类才能有public
3、只有主类里面才能写main方法
4、类里面不能进行行为的调用

匿名对象:
没有名称的对象叫做匿名对象,例如:
new Car();
New Person();
当对象的方法仅进行一次调用的时候
例如:

public class Computer
 {
 public String type=“HP-001”;
 public void speaking()
 {
 System.out.println(“计算机可以和人交谈”);
 }
 public static void main(String[] args) {
 new Computer().speaking();
 String s= new Computer().type;
 }
 }


二、构造方法

概念:构造对象的方法叫做构造方法
构造方法的使用规则:
(1)构造方法不能有返回值类型
(2)构造方法命名必须和类名保持一致
(3)只要对象一产生,就自动运行

public class Person
 {
 public Person(){
 System.out.println(“lily 出生了”);
 }
 public static void main(String[] args) {
 new Person();
 }}

有参数构造方法:

public class Person
 {
 public Person(int age,String name){
 System.out.println(“lily 出生了 age is:”+age);
 System.out.println(“lily 出生了 name is:”+name);}
 public void crying(){
 System.out.println(“crying…”);
 }
 public static void main(String[] args) {
 Person lily = new Person(1,“lily”);
 lily.crying();
 }
 }
 (4)如果该类没有定义任何一个构造方法,那么系统默认一个无参数构造方法
 注释:
 选中代码Ctrl+/
 例如:
 public class Person
 {
 public void crying(){
 System.out.println(“crying…”);
 }
 public static void main(String[] args) {
Person lily001 = new Person();

}
}
(5)如果类里面定义了有参数构造方法,那么在构造无参数的对象时候,该类必须显式指定无参数
构造方法
例如:下列构造无参数的对象的方法是错误的

public class Person
 {public Person(int age,String name){
 System.out.println(“lily 出生了 age is:”+age);
 System.out.println(“lily 出生了 name is:”+name);}
 public void crying(){
 System.out.println(“crying…”);
 }
 public static void main(String[] args) {
 Person lily = new Person(1,“lily”);
 lily.crying();
 Person lily001 = new Person();//错误,应该显式的指定无参数构造方法
 }
 }
 下列是正确的:
 public class Person
 {
 public Person()
 {}
 public Person(int age,String name){
 System.out.println(“lily 出生了 age is:”+age);
 System.out.println(“lily 出生了 name is:”+name);}
 public void crying(){
 System.out.println(“crying…”);
 }
 public static void main(String[] args) {
 Person lily = new Person(1,“lily”);
 lily.crying();
 Person lily001 = new Person();
 }
 }
 (6)构造方法的作用:可以用来初始化对象的一些属性
 示例1:
 public class Person
 {
 public String sex;
 public String name;
 public Person()
 {
 sex=“Female”;
 name=“lily”;
 }public void crying(){
 System.out.println(“crying…”);
 }
 public static void main(String[] args) {
Person p = new Person();
    System.out.println(p.sex);
    System.out.println(p.name);

}
}
示例2:

public class Person
 {
 public String sex;
 public String name;
 public Person(String _sex,String _name)
 {
 sex= _sex;
 name=_name;
 }public void crying(){
 System.out.println(“crying…”);
 }
 public static void main(String[] args) {
Person p = new Person("female","lily");
    System.out.println(p.sex);
    System.out.println(p.name);

}
}
1、构造函数:创建对象时候所用的函数为构造函数,可以对对象进行初始化。
2、一个类中如果没有定义过构造函数,则会有一个默认的空构造函数,如果类中定义了构造函数,那么默认的构造函数就没有了
3、构造函数和一般函数的区别
(1)构造函数:对象创建时候调用,并对对象初始化
一般函数:对象创建后,需要时候才调用
(2)构造函数:对象创建时,只调用一次
一般函数:对象创建后,可以多次调用
三、自定义方法

带static的方法不是对象的行为
该行为调用的语法:
类名.方法名();
示例:

public class Computer {
 public static void computing()
 {
 int count = 0; //定义水仙花数的个数
 for(int i=100;i<=999;i++)
 {
int b = i/100;    //取得百位数
        int s = i%100/10; //取得十位数
        int g = i%10;     //取得个位数
        
        
        if(i==g*g*g+s*s*s+b*b*b)
        {
            count++; //每次符合水仙花数条件,则x+1;
            System.out.println(i+" ");    //输出符合条件的数
        }
    }
    System.out.println("水仙花数总共有"+count+"个");    //输出水仙花数的总数    	
}
public static void main(String[] args) 
{
	
	Computer.computing();
}

}

(1)无参数的、无返回值的方法
语法:

修饰符 void 方法名()
 {}
 调用的语法:
 先生成对象–>对象.方法名();
 示例:
 public class Computer {
 public void display()
 {
 int count = 0; //定义水仙花数的个数
 for(int i=100;i<=999;i++)
 {
int b = i/100;    //取得百位数
        int s = i%100/10; //取得十位数
        int g = i%10;     //取得个位数
        
        
        if(i==g*g*g+s*s*s+b*b*b)
        {
            count++; //每次符合水仙花数条件,则x+1;
            System.out.println(i+" ");    //输出符合条件的数
        }
    }
    System.out.println("水仙花数总共有"+count+"个");    //输出水仙花数的总数    	
}
public static void main(String[] args) 
{
	
	Computer c1 = new Computer();
	c1.display();
}

}

(2)无参数的、有返回值的方法
语法:
修饰符 返回值类型[int、float、String…] 方法名()
{
return int、float、String…;
}
调用的语法:
a 先生成对象
b 数据类型 变量=对象.方法名();
示例:
public class Computer {
public int display()
{
int count = 0; //定义水仙花数的个数
for(int i=100;i<=999;i++)
{

int b = i/100;    //取得百位数
        int s = i%100/10; //取得十位数
        int g = i%10;     //取得个位数            
        
        if(i==g*g*g+s*s*s+b*b*b)
        {
            count++; //每次符合水仙花数条件,则x+1;
            System.out.println(i+" ");    //输出符合条件的数
        }
    }
    return count;
}
public static void main(String[] args) 
{
	
	Computer c1 = new Computer();
	int num=c1.display();
    System.out.println("水仙花数总共有"+num+"个");    //输出水仙花数的总数

}

}
(3)有参数的、无返回值的方法
语法:
修饰符 void 方法名(类型1 参数1,类型2 参数2…)
{

}
调用的语法:
a 先生成对象
b 对象.方法名(值1,值2…);
示例:
public class Machine {

public void makeJuice(String materials)
{
	System.out.println("榨"+materials+"汁");
}
public static void main(String[] args) {
	// TODO Auto-generated method stub
   Machine machine = new Machine();
   machine.makeJuice("apple");
   machine.makeJuice("plum");
   machine.makeJuice("watermelon");
   machine.makeJuice("strawberry");
}

}
(4)有参数的、有返回值的方法
语法:

修饰符 返回值类型[int、float、String…] 方法名(类型1 参数1,类型2 参数2…)
{
return int、float、String…;
}

调用的语法:
a 先生成对象
b 类型 变量=对象.方法名(值1,值2…);

示例:
public class Machine {

public String makeJuice(String materials)
{
	return "榨"+materials+"汁";
}
public static void main(String[] args) {
	// TODO Auto-generated method stub
   Machine machine = new Machine();
   String apple=machine.makeJuice("apple");
   String plum=machine.makeJuice("plum");
   String watermelon=machine.makeJuice("watermelon");
   String strawberry=machine.makeJuice("strawberry");
   System.out.println(apple);
   System.out.println(strawberry);
}

}

(5)返回值是引用数据类型

示例:
public class Computer {
public String type;
public String maker;
}

public class Machine {

public Computer makeProgram()
{
	Computer  c=new Computer();
	c.type="HP-001";
	c.maker="惠普";
	return c;
}
public static void main(String[] args) {
     Machine machine = new Machine();
     Computer computer=machine.makeProgram();
     System.out.println(computer.maker);
}

}
(6)参数是引用数据类型
示例:
public class Machine {

public void makeProgram(Computer c)
{
	System.out.println("该计算机的型号是:"+c.type);
	System.out.println("该计算机的厂商是:"+c.maker);

}
public static void main(String[] args) {
     Machine machine = new Machine();
     Computer ccc=new Computer();
     ccc.type="ThinkPad-T430";
     ccc.maker="lenovo";
     machine.makeProgram(ccc);
     
}

}