object oriented
1. Concept
As a developer must be familiar with and use, object-oriented (Object Oriented, OO) is a software development method . Object-oriented concepts and applications have gone beyond program design and software development to fields such as database systems , interactive interfaces, application structures, application platforms, distributed systems , network management structures, CAD technology, and artificial intelligence . Object-oriented is a method of understanding and abstracting the real world, and it is the product of the development of computer programming technology to a certain stage. Object-oriented thinking has been involved in all aspects of software development. For example, object-oriented analysis (OOA, Object Oriented Analysis), object-oriented design (OOD, Object Oriented Design), and what we often call object-oriented programming (OOP, Object Oriented Programming).
2. Object-Oriented Basics
1 Classes and Objects
Class: an abstract description of something in reality, an object is a concrete existence of this class of things
Inappropriate example: int x = 90; // int is class, x is object
// There are only two things in a class, fields (properties, member variables), and methods (functions)
// Example 1, design a student class and use
class Student{
int age; //this is the property
String name;
String school;
void speak() {//Behavior, method
System.out.println("My name is: "+name+"My age is: "+age+"My school is: "+school);
}
void study(){
System.out.println("Students are studying");
}
// System.out.println("This code will go wrong"); error, the execution statement cannot be written directly in the class
}
class Test
{public static void main(String [] args)
{
//Use the Student class to define an object stu
Student stu=new Student();
stu=null;
//manipulate the properties in the object
stu.age = 23;
stu.name="Zhang San";
stu.school="内大";
// call the method of the object
stu.speak();
stu.study();
}
}
//Example 2 design a Cat (cat) class and use
public class Cat {// cat propertiesString nickName;int age;String home;String owner;
void catchMouse(){System.out.println("Caught a little mouse");}void shout(){System.out.println("喵~~~");}void introduce() {System.out.println("My name is: "+nickName);System.out.println("My age is "+age);System.out.println("My home is: "+home);System.out.println("My owner:"+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.锄草();
}
}