java基础 面向对象

1.     Java基础:

Java语言基础

OOP(面向对象):Object oriented programming

    Java语言是面向对象的语言

    C面向过程

API

2.     面向对象程序设计

 

2.1面向过程的结构化程序设计

案例:打印员工信息

   

面向过程编程思想:更接近于计算机的执行过程,对于程序员来说编写并不是很方便。

面向对象编程思想:更接近于人的思维方式,对于程序员来说编写更加方便。

 

优化:

    将数据和方法封装为一个整体,java中将其称为类

面向过程思想实现

/*

 * 打印员工信息:

 * id   name  age  salary

 * 面向过程编程思想:代码编写更接近于计算机的执行过程

 */

public class PrintEmpInfo {

   public static void main(String[] args) {

      //定义一个员工的信息

      int id = 1;

      String name = "张三";

      int age = 23;

      double salary = 8000.0;

      //打印员工信息

      PrintEmpInfo info = new PrintEmpInfo();

      info.printInfo(id, name, age, salary);

   }

  

   //打印员工信息

   public void printInfo(int id,String name,int age,double salary){

      System.out.println("该员工的信息是:工号:"+id

            +",姓名:"+name+",年龄:"+age+",工资:"+salary);

   }

 

}

面向对象思想实现

Emp类

/*

 * 员工类

 */

public class Emp {

   int id;

   String name;

   int age;

   double salary;

 

   //描述一些行为/功能

   public void printEmpInfo(){

      System.out.println("员工信息:工号:"+id

            +",姓名:"+name+",年龄:"+age+",工资:"+salary);

   }

}

测试类

/*

 * 打印员工信息

 */

public class PrintEmpInfoObject01 {

   public static void main(String[] args) {

      //定义一个员工对象

      Emp emp = new Emp();

      emp.id = 3;

      emp.name = "张丽";

      emp.age = 24;

      emp.salary = 10000;

     

      //打印员工信息

      emp.printEmpInfo();

   }

 

}

 

2.2 类和对象

    什么是类:类是一类事物的模板

       类的成员:成员变量(属性/数据)   方法

 

    什么是对象(实例):

       是一个具体存在的个体/实例

 

 

    类和对象的关系:

       对象是经由类这个模板创建出来的一个具体的个体

/*

 *人类

 * 类的成员:

 *    属性(成员变量) /数据

 *    方法:描述一种功能/行为

 * 类:某一类事物的模板

 */

public class Person {

   String name;

   int age;

   char gender;

  

   public void walk(){

      System.out.println(name+"在散步!");

   }

 

}

 

/*

 * 测试类

 * 定义张丽对象,输出此人在散步

 */

public class PersonTest {

   public static void main(String[] args) {

      //定义张丽

//    Person person = new Person(); //创建person对象

//    //属性值是属于对象的,而不是属于类的

//    person.name = "张丽";

//    person.age = 23;

//    person.gender = '女';

     

      //创建一个person对象,不访问对象内部的内容

      new Person();

   }

 

}

   

 

2.3对象的创建和访问

 

1.  对象的创建:

a)   new  类名()

b)   Person per = new Person()

                         i.      等号右边是真正的创建对象,左边部分定义一个变量,指向此对象,此变量的意义:用于访问对象内部的内容使用。

2.  引用类型变量

a)   简称为引用

3.  访问对象的成员变量,方法

a)   引用是用于访问对象内部的(非static)成员变量,(非static)方法

 

在一个类中定义的非static的成员变量和方法是属于对象的。

 

 

练习:定义shoot项目中的对象类

  定义类:属性   行为

     出现的对象类:

         Hero:英雄机类

            数据(属性):

int width 

int height 

int x  

int y

int life

int doubleFire

            行为:

                step():移动行为

           Airplane:小敌机类

              属性:width height  x  y  step

              行为:step()

           BigAirplane:大敌机类

              属性:width  height   x  y  step

              行为:step()

           Bee:蜜蜂类

              属性:width  height  x  y  xstep  ystep

              行为:step()

           Sky:背景天空类

              属性:widht  height  x  y  y1  step

              行为:step()

           Bullet:子弹类

              属性:width height   x  y  step

              行为:step()

           World:程序的启动类

              main:

                  创建飞行物对象以及给对象属性赋值,然后让对象移动

Hero类

public class Hero {

   int width;

   int height;

   int x;

   int y;

   int life;

   int doubleFire;

  

   //移动功能

   public void step(){

      System.out.println("英雄机移动了!");

   }

}

Airplane类

public class Airplane {

   int width;

   int height;

   int x;

   int y;

   int step;

  

   //移动行为

   public void step(){

      System.out.println("小敌机移动了!");

   }

 

}

BigAirplane类

public class BigAirplane {

   int width;

   int height;

   int x;

   int y;

   int step;

  

   //移动行为

   public void step(){

      System.out.println("大敌机移动了!");

   }

 

}

Bee类

public class Bee {

   int width;

   int height;

   int x;

   int y;

   int xstep;

   int ystep;

  

   //移动行为

   public void step(){

      System.out.println("小蜜蜂移动了!");

   }

 

}

Sky类

public class Sky {

   int width;

   int height;

   int x;

   int y;

   int y1;

   int step;

  

   //移动行为

   public void step(){

      System.out.println("天空移动了!");

   }

 

}

World类

/*

 * 飞机大战程序启动类

 */

public class World {

   public static void main(String[] args) {

      //创建窗体

      //创建飞行物对象

      Hero hero = new Hero();

      hero.width = 97;

      hero.height = 124;

      hero.x = 140;

      hero.y = 400;

      hero.life = 3;

      hero.doubleFire = 0;

     

      //创建Sky背景对象

      Sky sky = new Sky();

      sky.width = 400;

      sky.height = 700;

      sky.x = 0;

      sky.y = 0;

      sky.y1 = -700;

      sky.step = 1;

     

      //创建一个子弹对象

      Bullet bullet = new Bullet();

      bullet.width = 8;

      bullet.height = 14;

      bullet.x = 200;

      bullet.y = 500;

      bullet.step = 2;

     

      //创建小敌机对象,大敌机对象,蜜蜂对象各3个

      Airplane[]  airplanes = new Airplane[3];

      BigAirplane[] bigAirplanes = new BigAirplane[3];

      Bee[] bees = new Bee[3];

     

      for(int i=0;i<3;i++){

         Airplane airplane = new Airplane();

         airplane.width = 49;

         airplane.height = 36;

         airplane.x = (int)(Math.random()*(400-airplane.width));

         airplane.y = - airplane.height;

         airplane.step =2;

         airplanes[i] = airplane;

         //创建大敌机对象

         BigAirplane airplane2 = new BigAirplane();

         airplane2.width = 69;

         airplane2.height = 99;

         airplane2.x = (int)(Math.random()*(400-airplane2.width));

         airplane2.y = -airplane2.height;

         airplane2.step = 2;

         bigAirplanes[i] = airplane2;

        

         //创建蜜蜂对象

         Bee bee = new Bee();

         bee.width = 60;

         bee.height = 50;

         bee.x = (int)(Math.random()*(400-bee.width));

         bee.y = -bee.height;

         bee.xstep = 1;

         bee.ystep = 2;

         bees[i] = bee;

      }

      //在控制台模拟飞行物对象移动效果

      hero.step();

      sky.step();

      bullet.step();

      for(int i=0;i<3;i++){

         airplanes[i].step();

         bigAirplanes[i].step();

         bees[i].step();

      }

     

   }

 

}

 

 

 

3.方法的重载

 

    

4.构造方法:

    语法结构:

 

    作用:

 

    分类:

       无参构造方法

      

       有参构造方法:

           this关键字:

- this关键字用在方法体中,用于指向调用该方法的当前对象;简单的说:哪个对象调用方法,this指的就是哪个对象。

- 为了方便起见,在没有歧义的情况下可以省略this:

       构造方法的重载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值