面向对象

面向对象思想:
面向对象是基于面向过程的思想
面向过程:强调每一个功能的步骤
面向对象:强调的是对象,由对象去调用功能

面向对象的特点:
1.更符合人们的思考习惯
2.将复杂的事情简单化
3.将我们从执行者变成指挥者

类与对象
1.编程为了什么?
为了把现实生活中的事物信息化,模拟出来
2.现实生活描述事物
属性:描述信息
行为:事物能做什么
3.java最基础单位是类(class)
成员变量:属性
成员方法:行为
4.成员变量:类中方法外
成员方法:去掉static
5.类与对象
我们是通过类来描述现实生活中的事物
类:一组具有相关的属性和行为的集合
对象:事物的具体体现
举例:
类:学生
对象:某某

package org.lanqiao;
/*

  • 属性:品牌(brand),价格(price),CPU

  • 行为:上网,打游戏,看电影
    */
    public class Computer {

    String brand;

    double price;

    String cpu;

    public void surfNet() {
    System.out.println(“上网查资料”);
    }

    public void palyGame() {
    System.out.println(“今晚吃鸡,大吉大利”);
    }

    public void watchMovie(String name) {
    System.out.println(“与”+name+“一起看电影”);
    }
    }

package org.lanqiao;

public class ComputerDemo {

public static void main(String[] args) {
	Computer computer = new Computer();
	
	computer.brand = "lenove";
	computer.price = 9999.99;
	computer.cpu = "i7878";

	System.out.println(computer.brand);
	System.out.println(computer.price);
	System.out.println(computer.cpu);
	
	computer.surfNet();
	computer.palyGame();
	computer.watchMovie("猪猪佩琪");
}

}

package org.lanqiao;
/*

  • 成员变量和局部变量的区别
  • 1.位置不同
  •  成员变量:类中方法外
    
  •  局部变量:方法中或者方法声明上
    
  • 2.内存中位置不同
  •  成员变量:堆里面
    
  •  局部变量:栈里面
    
  • 3.生命周期不同
  •  成员变量:随对象创建而存在,随对象消失而消失
    
  •  局部变量:随方法的调用而存在,随方法调用完成而消失
    
  • 4.初始化值不同
  •  成员变量:有默认值
    
  •  局部变量:没有默认值,必须先赋值,再使用
    

*/
public class Demo4 {

public static void main(String[] args) {
	Variable v = new Variable();
	
	v.show();
}

}

package org.lanqiao;
/*

  • 属性:姓名,性别,年龄,学号,成绩

  • 行为:学习,吃饭,打游戏

  • 成员变量

  • 成员方法
    */
    public class Student {

    //成员变量
    String name;

    String sex;

    int age;

    double score;

    //成员方法
    public void study() {
    System.out.println(“好好学习”);
    }

    public void eat() {
    System.out.println(“好好吃饭”);
    }

    public void playGame(String name) {
    System.out.println(name+“打游戏弄死”+name+"!!!");
    }
    }


package org.lanqiao;
/*

  • private:私有的
  •  一个修饰符
    
  •  修饰成员变量,成员方法
    
  •  被private修饰的成员只能被本类访问
    
  • 属性私有化,通过公共的get/set方法去访问
  • 封装
  • 面向对象3大特征之一
  • 原则
  • 将不需要对外提供的内容隐藏起来
  • 属性隐藏,提供公共的访问方式
  • 好处
  • 提高安全性
  • 代码复用性
    */
    public class Demo5 {
    public static void main(String[] args) {
    Person person = new Person();

// System.out.println(person.getName());
// System.out.println(person.getAge());

	person.setName("小肛");
	person.setAge(19);
	
	System.out.println(person.getName());
	System.out.println(person.getAge());
	
	person.sleep();
}

}

package org.lanqiao;
/*

  • 局部变量隐藏成员变量(就近原则)
  • 关键字this
  • this所在类的对象引用
  •  方法被谁调用,this就代表那个对象
    
  • 使用场景
  •  局部变量隐藏成员变量
    

*/
public class Person {

private String name;

private int age;

public String getName() {
	return name;
}

public void setName(String name) {
	this.name = name;
}

public int getAge() {
	return age;
}

public void setAge(int age) {
	if(age<0 || age>120) {
		System.out.println("你设置的年龄不合法");
	}else {
		this.age = age;
		System.out.println("年龄设置成功");
	}
	
}

void sleep() {
	System.out.println("睡前一杯牛奶");
}

}

package org.lanqiao;

public class Demo6 {

public static void main(String[] args) {
	Pig peiqi = new Pig();

// System.out.println(peiqi.getColor());
// System.out.println(peiqi.getAge());

	Pig George = new Pig("粉色");

// System.out.println(George.getColor());
// System.out.println(George.getAge());

	Pig yezhu = new Pig(5);

// System.out.println(yezhu.getColor());
// System.out.println(yezhu.getAge());

	Pig heizhu = new Pig("黑色",3);
	System.out.println(heizhu.getColor());
	System.out.println(heizhu.getAge());
}

}

package org.lanqiao;
/*

  • 构造方法

  •  给对象的数据进行初始化
    
  • 格式:

  •  1.方法名和类名相同
    
  •  2.没有返回值类型,连void都没有
    
  •  3.没有具体返回值
    
  • 如何调用

  •  new 构造方法(...)
    
  • 如果我们不提供构造方法,系统会给一个默认的构造方法

  • 如果我们提供了构造方法,系统不再为我们提供默认的构造方法
    */
    public class Pig {

    private String color;

    private int age;

    public Pig() {
    }

    public Pig(String color) {
    this.color = color;
    }

    public Pig(int age) {
    this.age = age;
    }

    public Pig(String color , int age) {
    this.color = color;
    this.age = age;
    }

    public String getColor() {
    return color;
    }

    public void setColor(String color) {
    this.color = color;
    }

    public int getAge() {
    return age;
    }

    public void setAge(int age) {
    this.age = age;
    }
    }


package org.lanqiao;

public class Car {
private String brand;

private String type;

private double price;

public Car() {
	
}

public Car(String brand , String type , double price) {
	this.brand = brand;
	this.type = type;
	this.price = price;
}

public String getBrand() {
	return brand;
}

public void setBrand(String brand) {
	this.brand = brand;
}

public String getType() {
	return type;
}

public void setType(String type) {
	this.type = type;
}

public double getPrice() {
	return price;
}

public void setPrice(double price) {
	this.price = price;
}

public void run() {
	System.out.println("兜风");
}

public void stop() {
	System.out.println("停车坐爱枫林晚");
}

}

package org.lanqiao;

public class Demo7 {

public static void main(String[] args) {
	Car bmw = new Car();
	bmw.setBrand("宝马");
	bmw.setType("SUV");
	bmw.setPrice(999999.00);
	
	System.out.println(bmw.getBrand());
	System.out.println(bmw.getType());
	System.out.println(bmw.getPrice());
	
	
	Car baojun = new Car("宝骏", "拖拉机", 998);
	System.out.println(baojun.getBrand());
	System.out.println(baojun.getType());
	System.out.println(baojun.getPrice());
	
	bmw.stop();
}

}

/*

  • 动物实体类
    */
    public class Animal {
    //实例变量
    String type;//类型
    String color;//颜色

    //类变量
    static String sex;

    public void eat() {
    System.out.println(“狗吃屎”);
    }
    }

/*

  • 变量
  •  成员变量
    
  •  	类变量
    
  •  	实例变量
    
  •  局部变量
    
  • 实例==对象
  • 类变量与实例变量
  •  类变量就是静态成员变量
    
  •  	位置:类中方法外
    
  •  	内存区域:方法区
    
  •  	生命周期:随类加载而加载
    
  •  	特点:无论创建多少对象,类变量仅在方法区中存在一份
    
  •  实例变量非静态成员变量
    
  •  	位置:类中方法外
    
  •  	内存区域:堆内存
    
  •  	生命周期:随对象创建而加载
    
  •  	特点:每创建一个对象就存在一个实例变量
    

*/
public class Demo1 {
public static void main(String[] args) {
Animal dog = new Animal();
dog.type = “犬科”;
dog.color = “金色”;

	Animal cat = new Animal();
	cat.type = "猫科";
	cat.color = "黑色";
}

}

/*

  • 代码块
  •  局部代码块:存在于方法中,控制变量生命周期
    

*/
public class Demo2 {
public static void main(String[] args) {
//int a =10;

	//局部代码块
	{
		int a = 10;
		for (int i = 0; i < 5; i++) {
			System.out.println("王老板最帅!!!");
		}
		//System.out.println(a);
	}
	//System.out.println(a);//超出了a的作用域
	
}

}

/*

  • 构造代码块:在类中成员的位置
  •  在构造方法之前执行,先执行构造代码块,再执行构造方法
    

*/
public class Demo3 {
public static void main(String[] args) {
Plane 波音747 = new Plane();

	Plane 波导 = new Plane("6寸","战斗机");
}

}

public class Boss {
String name ;
int age;

//构造代码块
{
	System.out.println("我是构造代码块");
}

//静态代码块
static {
	System.out.println("我是静态代码块");
}

/*

  • 静态代码块:
  •  类中成员位置,被static修饰
    
  •  随类加载而加载,只加载一次
    

*/
public class Demo4 {
public static void main(String[] args) {
Boss 小肛 = new Boss();

	Boss 宇航员 = new Boss("小谢",19);
}

}

public class Plane {
String size;
String type;

public Plane() {
	super();
	System.out.println("我是无参构造");
}

public Plane(String size, String type) {
	super();
	this.size = size;
	this.type = type;
	System.out.println("你是有参构造");
}

//构造代码块
{
	for (int i = 0; i < 5; i++) {
		System.out.println("出山");
	}
	System.out.println("花粥");
}	

}

public Boss() {
	super();
	System.out.println("我是无参构造");
}
public Boss(String name, int age) {
	super();
	this.name = name;
	this.age = age;
	System.out.println("我是有参构造");
}		

}

/* 下列是完整代码
*属性:name id salary bonus
*行为:work
*private:私有化,是属性私有化,必须通过get/set方法去访问
*/
package org.lanqiao;
public class Manager2 {
private String name;
private int id;
private int salary;
private int bonus;
public Manager2() {
}
public Manager2(String name,int id,int salary,int bonus) {
this.name = name;
this.id = id;
this.salary = salary;
this.bonus = bonus;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public int getBonus() {
return bonus;
}
public void setBonus(int bonus) {
this.bonus = bonus;
}
public void Work() {
System.out.println(“的项目经理正在努力的做着管理工作,分配任务,检查员工提交上来的代码…”);
}
}

package org.lanqiao;
public class Manager3 {
public static void main(String[] args) {
Manager2 man = new Manager2();
man.setName("");
man.setId(123);
man.setSalary(15000);
man.setBonus(6000);
System.out.print(man.getName());
System.out.print(“工号为”+man.getId());
System.out.print(“基本工资为”+man.getSalary());
System.out.print(“奖金为”+man.getBonus());
man.Work();
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值