编程语言
首页 > 编程语言> > JAVA-(2)-学习Java语言初级(三)类和对象.

JAVA-(2)-学习Java语言初级(三)类和对象.

作者:互联网

前面学了基础知识,现在可以写自己的类了,

类: 声明变量(members),方法(methods),构造器(constructors)

对象:实例化对象(Instantiate an object)用 dot 操作对象里的变量和方法。

更多类知识:this 关键字的用法

嵌套类:Nested Classes

枚举类型:define and use sets of constants

第一节:类

public class Bicycle {
        
    // the Bicycle class has
    // three fields
    public int cadence; //节奏
    public int gear; //档位
    public int speed;
        
    // the Bicycle class has
    // one constructor
    public Bicycle(int startCadence, int startSpeed, int startGear) {
        gear = startGear;
        cadence = startCadence;
        speed = startSpeed;
    }
        
    // the Bicycle class has
    // four methods
    public void setCadence(int newValue) {
        cadence = newValue;
    }
        
    public void setGear(int newValue) {
        gear = newValue;
    }
        
    public void applyBrake(int decrement) {
        speed -= decrement;
    }
        
    public void speedUp(int increment) {
        speed += increment;
    }
        
}
public class MountainBike extends Bicycle {
        
    // the MountainBike subclass has
    // one field
    public int seatHeight;

    // the MountainBike subclass has
    // one constructor
    public MountainBike(int startHeight, int startCadence,
                        int startSpeed, int startGear) {
        super(startCadence, startSpeed, startGear);
        seatHeight = startHeight;
    }   
        
    // the MountainBike subclass has
    // one method
    public void setHeight(int newValue) {
        seatHeight = newValue;
    }   

}
class MyClass extends MySuperClass implements YourInterface {
    // field, constructor, and
    // method declarations
}

MyClass 是子类,MySuperClass是基类 后面是实现接口,里面的变量会有Modifiers,来实现控制。

类名的第一个字母应该大写,并且方法名中的第一个(或唯一一个)单词应该是动词

public double calculateAnswer(double wingSpan, int numberOfEngines,
                              double length, double grossTons) {
    //do the calculation here
}

上面是定义的方法,包含6元素:Modifiers(public),returnType(void),name,parameter, ExceptionList(例外表)方法

考点:the method signature—the method's name and the parameter types.    calculateAnswer(double, int, double, double)

方法的命名规则:动词小写开头,接或不接 形容词名词大写开头。名词唯一,只有在overloading的时候才会有重名的。

如:run  、 runFast、getBackground、getFinalData、compareTo、setX、isEmpty

重载(Overloading Methods):尽量少用(可读性)

public class DataArtist {
    ...
    public void draw(String s) {
        ...
    }
    public void draw(int i) {
        ...
    }
    public void draw(double f) {
        ...
    }
    public void draw(int i, double f) {
        ...
    }
}

构造器:与类同名切无return type.,亦可重载(different argument list) 

public Bicycle(int startCadence, int startSpeed, int startGear) {
    gear = startGear;
    cadence = startCadence;
    speed = startSpeed;
}

//create a new Bicycle object called myBike, a constructor is called by the new operator:
Bicycle myBike = new Bicycle(30, 0, 8);

基类有无参数构造器的时候子类可自动继承该构造器。无需在写。

Note: If you want to pass a method into a method, then use a lambda expression or a method reference.

无穷参数: an ellipsis (three dots, ...), then a space, and the parameter name.

public Polygon polygonFrom(Point... corners) {
    int numberOfSides = corners.length;
    double squareOfSide1, lengthOfSide1;
    squareOfSide1 = (corners[1].x - corners[0].x)
                     * (corners[1].x - corners[0].x) 
                     + (corners[1].y - corners[0].y)
                     * (corners[1].y - corners[0].y);
    lengthOfSide1 = Math.sqrt(squareOfSide1);

    // more method body code follows that creates and returns a 
    // polygon connecting the Points
}

eg:
public PrintStream printf(String format, Object... args)

System.out.printf("%s: %d, %s%n", name, idnum, address);
System.out.printf("%s: %d, %s, %s, %s%n", name, idnum, address, phone, email);

第二节:对象:

一下提供了一个程序,思考下如何继续完成他。

public class CreateObjectDemo {

    public static void main(String[] args) {
		
        // Declare and create a point object and two rectangle objects.
        Point originOne = new Point(23, 94);
        Rectangle rectOne = new Rectangle(originOne, 100, 200);
        Rectangle rectTwo = new Rectangle(50, 100);
		
        // display rectOne's width, height, and area
        System.out.println("Width of rectOne: " + rectOne.width);
        System.out.println("Height of rectOne: " + rectOne.height);
        System.out.println("Area of rectOne: " + rectOne.getArea());
		
        // set rectTwo's position
        rectTwo.origin = originOne;
		
        // display rectTwo's position
        System.out.println("X Position of rectTwo: " + rectTwo.origin.x);
        System.out.println("Y Position of rectTwo: " + rectTwo.origin.y);
		
        // move rectTwo and display its new position
        rectTwo.move(40, 72);
        System.out.println("X Position of rectTwo: " + rectTwo.origin.x);
        System.out.println("Y Position of rectTwo: " + rectTwo.origin.y);
    }
}

完成你的程序并核对结果如下,结果的下面会提供正确的代码。

Width of rectOne: 100
Height of rectOne: 200
Area of rectOne: 20000
X Position of rectTwo: 23
Y Position of rectTwo: 94
X Position of rectTwo: 40
Y Position of rectTwo: 72
 ok,我碰到下面的问题:  int x ; 这里是否需要付给初始值? this.x =x 这里是否需要this这个关键字? 如何同时编译这3个class,、否则提示:

垃圾收集器:  Java运行时环境有一个垃圾收集器,它定期释放不再被引用的对象使用的内存。垃圾收集器在确定时间正确时自动执行其工作,意思是不像其他语言,Java无需自己处理垃圾,

上面的代码把this去掉就可以运行了。

 

 

 

 

 

 

 

 

 

 

标签:rectTwo,JAVA,rectOne,int,corners,void,初级,Java,public
来源: https://blog.csdn.net/ldy889/article/details/102746716