面向对象(个人笔记九)

面向对象

一、概念

作为开发人员必须熟知并运用的,面向对象(Object Oriented,OO)是软件开发方法。面向对象的概念和应用已超越了程序设计和软件开发,扩展到如数据库系统、交互式界面、应用结构、应用平台、分布式系统、网络管理结构、CAD技术、人工智能等领域。面向对象是一种对现实世界理解和抽象的方法,是计算机编程技术发展到一定阶段后的产物。面向对象的思想已经涉及到软件开发的各个方面。如,面向对象的分析(OOA,Object Oriented Analysis),面向对象的设计(OOD,Object Oriented Design)、以及我们经常说的面向对象的编程实现(OOP,Object Oriented Programming)。

二、面向对象基础

1 类与对象

类:对现实中某些事物的抽象描述, 对象,就是本类事物的一个具体存在

打个不恰当的例子:int x = 90;  // int是类,  x 是对象

// 类中只有两种东西, 字段(属性,成员变量),和方法(函数)
// 例一,设计一个学生类,并使用
class Student{
  int age;  //这个就是属性
  String name; 
  String school;
  void speak() {//行为,方法
  System.out.println("我的名字是: "+name+"我的年龄是: "+age+"我的学校是: "+school);
  }
  void study(){
  System.out.println("学生在学习");  
  }
  // System.out.println("这句代码放在这将出错"); 错误,类中不能直接写执行语句

}

 class Test

 {
  public static void main(String [] args)
  {
  //用Student这个类,定义了一个对象 stu
  Student stu=new Student();  
  stu=null;
  //操作对象中的属性
  stu.age=23;
  stu.name="张三"; 
  stu.school="内大";
  //调用对象的方法
  stu.speak();
  stu.study();

 }

}

//例二 设计一个Cat(猫)类,并使用
public class Cat  {
//猫的属性
String nickName;
int age;
String home;

String owner;

void catchMouse(){
System.out.println("逮到一只小老鼠");
}
void shout(){
System.out.println("喵~~~");
}
void introduce() {
System.out.println("我的名字是:"+nickName);
System.out.println("我的年龄是"+age);
System.out.println("我的家是:"+home);
System.out.println("我的主人:"+owner);

}}

class Test{public static void main(String[] args) {
Cat cat=new Cat();
  /*cat.nickName="咖啡猫";
cat.age=2;
cat.home="赵苗苗家";
cat.owner="赵苗苗"; 
*/
cat.catchMouse();
cat.shout();
cat.introduce();
}}

//例三 设计一个计算器类,能计算加减乘除
public class Calc {
int wdith=20;
int height=30;
String color="灰色"; 
//加
int jia(int a,int b){
return a+b;
}
//减
int jian(int a ,int b ){
return a-b;
}
//展示
void show(){
System.out.println("这是一个超级计算,宽"+wdith +"长" +height+"颜色"+color);
System.out.println("它能计算加法,和减法");
}
}

class Test2{  //the type Test is already defined 这个类型已经被定义了
public static void main(String[] args) {
Calc c=new Calc();
c.show();
int result1=c.jia(888283, 23434); //计算加法
int result2=c.jia(9239923, 1323); //计算减法
System.out.println("加法的结果是:"+result1);
System.out.println("减法的结果是:"+result2);
}
}
  //设计一个银行类,能存钱取钱,能查询余额  
public class Bank {
int 账户余额=0;
void 取钱(int count){
if(账户余额-count<0){
System.out.println("余额不足!");
}
else{
账户余额-=count;
System.out.println("取款成功!当前日期 "+new java.util.Date());
}
}
void 存钱(int count){
账户余额+=count;
System.out.println("存款成功!当前日期 "+new java.util.Date());
}
void 查询余额(){
System.out.println("当前余额:"+账户余额);
}
}
class Test3{
public static void main(String[] args) {
Bank bank=new Bank();
bank.取钱(5000);
bank.存钱(2000);
bank.存钱(5000);
bank.存钱(10000);
bank.取钱(4000);
bank.取钱(9000);
bank.取钱(500000);
}
}


2 对象与内存空间

Student stu=new Student(); 
stu=null 则  new Student(); 出来的实体将变成垃圾
关于初值 new 出来的对象实体,里面的属性会默认初始化
默认初值
int 型 : 0 
boolean : false
char : \u0000
float : 0.0f
doubl : 0.0
引用类型: null 

class Student{
String school;
void eat(){
 String shitang; //变量在使用之前必须赋初值
 System.out.println("学生去"+shitang+"吃饭");  //编译出错,因shitang不是类的属性,它是方法的局部变量
 }
  
 void gotoSchool(){
   System.out.println("学生去"+school +"上学"); //编译不出错,因为school是类的属性,它有默认初值,是null
 }
 }

//例子 对象做为函数参数
class Robot{
void 锄草(){
System.out.println("机器人把草地收拾干净了");
}
}

class Student{
  public static void main(String [] args){
   Robot robot=new Robot();
   干活(robot); //调用函数,并传参
  }
  
  static void 干活(Robot robot){//这个函数,接收的参数类型是  对象类型
 robot.锄草();
  }
}

匿名对象
class Student{
  public static void main(String [] args) {  
 // Robot robot=new Robot();
 // robot.锄草(); 
 new Robot().锄草();  //匿名对象,执行完后,实体马上就变成垃圾了
 //什么情况下用匿名对象?
 //1) 对象的方法或属性只访问一次
 //2) 对象做为函数的参数
    干活(new Robot());  //使用匿名对象做为函数的参数
  }
  
  static void 干活(Robot robot){
 robot.锄草();
  }
}

猜你喜欢

转载自blog.csdn.net/weixin_41205479/article/details/79951673
今日推荐