0
点赞
收藏
分享

微信扫一扫

java进阶-第1章 类与对象

小_北_爸 2022-04-05 阅读 69
java

1.1 用类创造对象

1.1.1 对象与类

1、对象: 对象是实体,需要被创建,可以为我们做事情

2、类: 类是规范,根据类的定义来创建对象

3、对象 = 属性+服务

.数据: 属性或状态

.操作: 函数(方法)

.封装: 把数据和对数据的操作放在一起

--操作紧紧地把数据包裹住

/**  画图过程所用的类  **/
/**入口**/
package shapes;
public class MyUp {   //该图为我自己绘制的一个“馋”的脸图
​
    public static void main(String[] args) {
        // TODO Auto-generated method stub
​
        Picture pic = new Picture(400,400);   //创建一个画布(我的理解)
        Circle c1 = new Circle(100,100,20);   //设置圆1的位置(x,y)和大小    可当做第四象限看待,但是数值为正,
        Circle c2 = new Circle(300,100,20);
        Circle c4 = new Circle(100,100,60);
        Circle c5 = new Circle(300,100,60);
        TuoYuan t1 = new TuoYuan(200,250,60,40);  //设置椭圆1的位置和x、y半径
        Circle c3 = new Circle(188,250,4);
        Circle c6 = new Circle(212,250,4);
        Circle c7 = new Circle(200,250,255);
        TuoYuan t2 = new TuoYuan(200,350,200,10);
        TuoYuan t3 = new TuoYuan(300,360,8,10);
        TuoYuan t4 = new TuoYuan(300,380,10,15);
        TuoYuan t5 = new TuoYuan(300,400,14,20);
        pic.add(c4);  //画圆4
        pic.add(c5);  //画圆5
        pic.add(t1);  //画椭圆1
        pic.add(c2);  //画圆2
        pic.add(c1);  //画圆1
        pic.add(c3);
        pic.add(c6);
        pic.add(c7);
        pic.add(t2);
        pic.add(t3);
        pic.add(t4);
        pic.add(t5);
        pic.draw();   //开始画图(我自己的理解)
    }
​
}
/**椭圆***/
package shapes;
​
import java.awt.Graphics;
​
public class TuoYuan extends Shape {
​
    private int x;
    private int y;
    private int xradius;  //x半径
    private int yradius;  //y半径
    
    public TuoYuan(int x, int y, int xradius, int yradius)
    {
        this.x = x;
        this.y = y;
        this.xradius = xradius;
        this.yradius = yradius;
    }
    @Override
    public void draw(Graphics g) {
        g.drawOval(x-xradius/2, y-yradius/2, xradius, yradius);
    }
​
}
/**圆***/
package shapes;
​
import java.awt.Graphics;
​
public class Circle extends Shape {
    private int x;
    private int y;
    private int radius;
    
    public Circle(int x, int y, int radius)
    {
        this.x = x;
        this.y = y;
        this.radius = radius;
    }
    @Override
    public void draw(Graphics g) {
        g.drawOval(x-radius, y-radius, radius*2, radius*2);
    }
}
/** 线 **/
package shapes;
​
import java.awt.Graphics;
​
public class Line extends Shape {
    private int x1;
    private int y1;
    private int x2;
    private int y2;
    
    public Line(int x1, int y1, int x2, int y2)
    {
        this.x1 = x1; this.y1 = y1;
        this.x2 = x2; this.y2 = y2;
    }
    
    @Override
    public void draw(Graphics g) {
        g.drawLine(x1, y1, x2, y2);
    }
​
}
/** 三角形 **/
package shapes;
​
import java.awt.Graphics;
​
public class Triangle extends Shape {
    private int[] x = new int[3];
    private int[] y = new int[3];
    
    public Triangle(int x1, int y1, int x2, int y2, int x3, int y3)
    {
        x[0] = x1; x[1] = x2; x[2] = x3;
        y[0] = y1; y[1] = y2; y[2] = y3;
    }
    
    @Override
    public void draw(Graphics g) {
        g.drawPolygon(x, y, x.length);
    }
​
}
​
/** 形状 **/
package shapes;
​
import java.awt.Graphics;
​
public abstract class Shape {
    
    public abstract void draw(Graphics g);
    
}
​
/** 长方形  **/
package shapes;
​
import java.awt.Graphics;
​
public class Rectangle extends Shape {
    private int x;
    private int y;
    private int width;
    private int height;
    
    public Rectangle(int x, int y, int width, int height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }
​
    @Override
    public void draw(Graphics g) {
        g.drawRect(x, y, width, height);
    }
​
}
​
/** 图  **/
package shapes;
​
import java.awt.Graphics;
import java.util.ArrayList;
​
import javax.swing.JFrame;
import javax.swing.JPanel;
​
public class Picture extends JFrame {
    private static final long serialVersionUID = 1L;
    private int width;
    private int height;
    
    private ArrayList<Shape> listShape = new ArrayList<Shape>();
    
    private class ShapesPanel extends JPanel {
        private static final long serialVersionUID = 1L;
​
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            for ( Shape s : listShape )
            {
                s.draw(g);
            }           
        }
        
    }
    
    public void add(Shape s)
    {
        listShape.add(s);
    }
​
    public Picture(int width, int height)
    {
        add(new ShapesPanel());
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.width = width;
        this.height = height;
    }
    
    public void draw()
    {
        setLocationRelativeTo(null);
        setSize(width, height);
        setVisible(true);
    }
}
/**  除了椭圆的绘制  **/
package shapes;
​
public class MyPic {
    public static void main(String[] args) 
    {
        Picture pic = new Picture(420,300);
        Circle c1 = new Circle(320,40,80);
        Rectangle r1 = new Rectangle(100, 100, 100, 100);
        Triangle t1 = new Triangle(100, 100, 200, 100, 150, 50);
        Line l1 = new Line(0,205,400,205);
        Circle c2 = new Circle(200,200,50);
        Circle c3 = new Circle(100,100,100);
        pic.add(c1);
        pic.add(r1);
        pic.add(t1);
        pic.add(l1);
        pic.add(c2);
        pic.add(c3);
        pic.draw(); 
    }
}
​

1.1.2 OOP特性

1、一切都是对象

2、程序就是一堆互相发送消息的对象

3、每个对象有自己的储存空间,里面是其他的对象

4、每个对象都有一个类型

5、所有属于某个特定类型的对象可以提供相同的服务

1.2 定义类

1、自动售货机

/**
 * 这个自动售货机里面假如只卖一种商品
 */
package verdingMachine;
​
public class verdingMachine {
​
    int price=80;    //原价
    int balance;     //结余
    int total;       //一天之内卖得的钱
    
    void showPrompt() {
        System.out.println("Welcome"); //欢迎使用售货机
    }
    
    void insertMoney(int amount) {  //顾客给的钱
        
        balance =balance + amount;
        
    }
    
    void showBalance() {  //找零
        System.out.println(balance);
    }
    
    void getFood() {  //给商品
        if(balance>=price) {
            System.out.println("here you are.");
            balance=balance-price; //余额
            total= total+price;  //每一次卖出所得的钱
        }
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
​
        verdingMachine vm = new verdingMachine();
        vm.showPrompt(); //礼貌用语
        vm.showBalance();  
        vm.insertMoney(1000);  //顾客给的钱
        vm.getFood();    //给商品
        vm.showBalance();  //找零
    }
​
}
 

2、 创建对象

(1)、对象变量是对象的管理者。

如上:创建一个类VendingMachine ,可作为一个类型的名字,可用来定义变量,用来定义一个变量如vm,这个变量就可以接收这个VendingMachine这个对象。

3、 让对象做事情

.运算符

[变量名字].[让对象做的事情]

4、 类是定义这个类的所有变量长什么样,而对象是这个类的一个个的具体的实例,在类里面所定义的变量,在每一个对象当中存在,并且在每一个对象当中都是不一样的。

1.3 成员变量和成员函数

1.3.1 成员变量

1、成员变量:类定义了对象中所具有的变量

2、每个对象有自己的变量,和同一个类的其他对象是分开的。

1.3.2 成员函数

1、函数与成员变量

在函数中可以直接写成员变量的名字来访问成员变量。

函数是通过对象来调用的。

v.insertMoney()

2、 this

this: 是成员函数的一个特殊的固有的本地变量,它表达了调用这个函数的那个对象。

int price = 80;
void setPrice(int price){
    this.price = price;//将上面的price赋值给函数内部的price,相当于在函数内部访问到上面的那个price
}
void showBalance(){
    System.out.println("this.balance"); 
}

(1)、 调用函数

.运算符 //调用某个对象

在成员函数内部直接调用自己(this)的其它函数。在成员函数外部必须用对象调用成员函数。

void insertMoney(int amount){
    balance = balance + amount;
    showBalance(); //在函数内部调用函数,不需要用this.showBalance();,当然也没错,如果记不住也可以this
}

3、 本地变量

本地变量: 定义在函数内部的变量。

本地变量作用域和生存期都是函数内部。

4、 成员变量 的生存期是对象的生存期,作用域是类内部的成员函数。(new 出一个对象之后,作用域和生存期在这个对象里面)

1.4 对象初始化

1、 成员变量定义初始化

成员变量在定义的地方就可以给出初始值。

没有给出初始值的变量会自动获得0值。

对象变量的0值,表示没有管理任何对象,也可以主动给null值。

定义初始化可以调用函数,甚至可以使用已经定义的成员变量。

2、 构造函数

在VendingMachine这个类里,创建一个VendingMachine函数

VendingMachine(){
    total = 0;
}  //构造函数,在构造的时候会被自动调用
    

如果有一个成员函数的名字和类的名字完全相同,则在创建这个类的每一个对象的时候会自动调用这个函数。

这个函数不能有返回类型。

3、 重载

函数名相同,参数不同。

一个类可以有多个构造函数,只要它们的参数表不同。

创建对象的时候给出不同的参数值,就会调用不同的构造函数。

通过this还可以调用其他构造函数。

VendingMachine(int price){ // 重载
    this.price = price;
}
 

1.5 关于编程

修改代码如下:

 

 

举报

相关推荐

0 条评论