Java Notes 程序猿的读书笔记

Java 7_Fields&Functions&Classes

2018-09-02  本文已影响0人  綿綿_

Basic class definition

public means this class can be used by other classes
Class names should begin with a capital letter
A file can't contain two public classes. It can contain classes that are not public
If you place class files in the same folder the java compiler will be able to find them

Java fields (class variables) and methods (functions)

public class Monster{
    
    // Class Variables or Fields
    // You declare constants with final
    
    public final String TOMBSTONE = "Here Lies a Dead monster";
    
    // private fields are not visible outside of the class
    private int health = 500;
    private int attack = 20;
    private int movement = 2;
    private int xPosition = 0;
    private int yPosition = 0;
    private boolean alive = true;
    
    // public variables are visible outside of the class
    // You should have as few as possible public fields
    public String name = "Big Monster";
    
    // Class Methods
    // Accessor Methods are used to get and set the values of private fields
    
    public int getAttack()
    {
        return attack;
    }
    
    public int getMovement()
    {
        return movement;
    }
    
    public int getHealth()
    {
        return health;
    }

Access Function

You can use the private variables inside of class, but cannot use it out of class without access function

public class JavaLessonSeven {
    
    public static void main(String[] args){
        
        // You create an object using the blueprint of this class as follows
        // className objectName = new className();
        
        Monster Frank = new Monster();
        
        // Since the objects name is public you can change it directly
        
        Frank.name = "Frank";
        
        // You retrieve class field values by listing the objectName.fieldName
        // You execute class methods by listing objectName.methodName()
        
        System.out.println(Frank.name + " has an attack value of " + Frank.getAttack()); 
  
    
    }
}

overloading a method

You can create multiple versions using the same method name
When overloading a method you can't just change the return type

    public void setHealth(int decreaseHealth)
    {
        health = health - decreaseHealth;
        if (health < 0)
        {
            alive = false;
        }
    }
    
    public void setHealth(double decreaseHealth)
    {
        int intDecreaseHealth = (int) decreaseHealth;
        health = health - intDecreaseHealth;
        if (health < 0)
        {
            alive = false;
        }
    }

The Constructor

Code that is executed when an object is created from this class definition
The method name is the same as the class
The constructor is only executed once per object
The constructor can't return a value
You can overload constructors like any other method

    public Monster(int health, int attack, int movement)
    {
        this.health = health;
        this.attack = attack;
        this.movement = movement;
        /* If the attributes had the same names as the class health, attack, movement
         * You could refer to the objects fields by proceeding them with this
         * this.health = health;
         * this.attack = attack;
         * objectFieldName = attributeFieldName;
         */
        
    }
    
    // The following constructor is the one provided by default if you don't create any other constructors
    // Default Constructor
    
    public Monster()
    {
        
    }

call other constructors

     * public LessonSeven(int newHealth)
     * {
     *      health = newHealth;
     * }
     * 
     * public LessonSeven(int newHealth, int newAttack)
     * {
     *      this(newHealth); // Any calls to another constructor must occur on the first line
     *      attack = newAttack;
     * }
     
    
}
上一篇下一篇

猜你喜欢

热点阅读