JAVA基础-面向对象思想

来源:互联网 发布:淘宝会员名怎么取不了 编辑:程序博客网 时间:2024/06/05 20:18
JAVA基础-面向对象思想
2017-7-05
1、面向对象实现计算器

2、什么是类?

 抽象性的东西  概念性的东西
对象  具体的  可见的
小猫的例子:
package zhangyu2017705;

public class work1{

public static class Cat{
     String name;
     String color;
     int age;

     public void eat( String fish){
          System.out.print(name+"eat"+fish);
          }
     }
     public static void main(String[] args) {
     // TODO Auto-generated method stub
     Cat cat=new Cat();
     cat.age=3;
     cat.color="白色";
     cat.name="小白";
     cat.eat("fish");
}

}
类定义了一种抽象数据类型

成员变量和局部变量的区别:


创建员工类:class emp
package zhangyu2017705;

public class work3_Emp {
     public static class Emp{
          String name;
          int age;
          char gender;
          double salary;

          public void work(){
          System.out.println("Working...");
          }

          public void print(){
              System.out.println("姓名  "+name+"  年龄  "+age+" 性别  "+gender+" 薪资  "+salary);
          }
     }

     public static void main(String[]args){
          Emp emp=new Emp();
          emp.name="张三";
          emp.age=23;
          emp.gender='男';
          emp.salary=6000;
          emp.print();
     }
}


3、面向过程的结构化程序设计

按照事情发展的顺序 原来的东西重用性不高

4、设置窗体 JFrame

package zhangyu2017705;

import java.awt.Dimension;

import javax.swing.JFrame;

public class work4_JFrame {
     public static void main(String[] args){
          JFrame jframe=new JFrame();
          jframe.setTitle("demo1");
          jframe.setSize(new Dimension(200,180));
          jframe.setVisible(true);
     }
}


5、

package homework2017703;

import java.util.Scanner;

import homework2017703.homework_Tetris.Cell;

public class TetrisGame {

     public static void print(Cell cell){
          for(int i=1;i<=20;i++){
              for(int j=1;j<=10;j++){
                   if(i==cell.rows&&j==cell.cols){
                        System.out.print("*");
                   }
                   else{
                        System.out.print("-");
                   }
              }
              System.out.println();
          }
     }

   public static void main(String[] args) {

          Cell cell=new Cell();
          cell.cols=1;
          cell.rows=4;
          //打印cell row 10 col 20
           print(cell);
          Scanner sc=new Scanner(System.in);
          int count;

          do{
              System.out.println("请输入操作数:(1-下降  2-左移   3-右移)");
              count=sc.nextInt();

          if(count==1){
              cell.drop();
          }
          if(count==2){
              cell.moveleft();
          }
          if(count==3){
              cell.moveright();
              }

           print(cell);

          }while(count!=0);

          if(count==0){
              System.out.println("游戏结束");
          }

          sc.close();

     }
}
原创粉丝点击