And hello to u too:) Click the icon above for more detail

Java On Call 2

  1. Java基础知识
  2. 面向对象
  3. 常用API
  4. 集合I/O
  5. 多线程、网络编程、反射、设计模式

这是准备java面试的第二天,以上的分类的思维导图,来自->这里

面向对象(Object Oriented)

import java.io.Serializable;

/**
 * A class to store Halloween information.
 * <p>
 * The year, the number of vistors, hourly temperatures in deg C and the weather condition is recorded. This class
 * has been created as a lecture example, and is not otherwise particularly useful!  
 * <p>
 * This version demonstrates the implementation of Comparable (for sorting) and Serializable (for
 * filing).  Also, the mutators for temperature and weather condition have been combined, so both
 * attributes have to be set at the same time.  In this way they cannot be set to an illegal value
 * independently.
 * 
 * @author Alan McLeod
 * @version 3.2
 */
public class Halloween5 implements Comparable<Halloween5>, Serializable {

	private static final long serialVersionUID = 4705089863030936649L;
	private int year;
    private int numMunchkins;
    private int[] temperatures;
    private String weatherCondition;

    /**
     * Full parameter constructor.
	 * 可以看到如果是和类名一样的方法的那他就是个constructor
     * @param yr The year when the data was collected.
     * @param numKids The number of Trick or Treaters!
     * @param temps The air temperatures in degrees Centigrade in an array of int of any size.
     * @param weather The weather condition: "clear", "snow" or "rain".
     * @throws IllegalHalloweenException If arguments are not legal.
     */
    // 4 parameter constructor invokes mutators
    public Halloween5(int yr, int numKids, int[] temps, String weather) throws IllegalHalloweenException {
        setYear(yr);
        setNumMunchkins(numKids);
        setWeather(temps, weather);
    } // end Halloween4 4 parameter constructor

    /**
     * Three parameter constructor.  The weather condition does not have to be supplied.
	 * 利用了方法过载
     * @param yr The year when the data was collected.
     * @param numKids The number of Trick or Treaters.
     * @param temps The air temperatures in degrees Centigrade in an array of int of any size.
     * @throws IllegalHalloweenException if arguments are not legal.
     */
    // 3 parameter constructor invokes 3 parameter constructor with an assumption about the
    // weatherCondition attribute
    public Halloween5(int yr, int numKids, int[] temps) throws IllegalHalloweenException {
        this(yr, numKids, temps, "unknown");
    } // end Halloween4 3 parameter constructor

    /**
     * Sets the year the data was recorded.
     * @param year The calendar year.
     * @throws IllegalHalloweenException if the year does not lie between 1959 and 2016
     */
    public void setYear(int year) throws IllegalHalloweenException {
        if (year < 1950 || year > 2019)
            throw new IllegalHalloweenException("Illegal year: " + year);
        this.year = year;
    } // end year mutator

    /**
     * Sets the number of kids.
     * @param numKids The number of kids arriving at the door.
     * @throws IllegalHalloweenException if the number of kids is less than zero or greater
     * than 500.
     */
    public void setNumMunchkins(int numKids) throws IllegalHalloweenException {
        if (numKids < 0 || numKids > 500)
            throw new IllegalHalloweenException("Illegal number of kids: " + numKids);
        numMunchkins = numKids;
    } // end numMunchkinds mutator

    /**
     * Sets the temperatures array and the weather condition String.  The temperatures are 
     * recorded with one temperature per hour.
     * @param temps An array of temperatures between -30 and 30 degrees C.
     * @param weather The weather condition as a String.
     * @throws IllegalHalloweenException if the condition is not "rain", "snow", "clear" or "unknown",
     * or if the array is empty or any temperatures are not legal. 
     */
    public void setWeather(int[] temps, String weather) throws IllegalHalloweenException {
        double avgTemperature = 0;
        if (temps.length == 0)
            throw new IllegalHalloweenException("No temperatures supplied");
        for (int temperature : temps) {
            if (temperature > 30 || temperature < -30)
                throw new IllegalHalloweenException("Illegal temperature in array: " + temperature);
            avgTemperature += temperature;
        }
        temperatures = temps.clone();
        avgTemperature = Math.round(10 * avgTemperature / temperatures.length) / 10.0;
        if ((weather.equalsIgnoreCase("rain") && avgTemperature > -5) ||
                (weather.equalsIgnoreCase("snow") && avgTemperature < 5) ||
                weather.equalsIgnoreCase("clear") || weather.equalsIgnoreCase("unknown")) {
            weatherCondition = weather;
        } else
            throw new IllegalHalloweenException("Illegal weather/temperature combination: " +
                    weather + ", " + avgTemperature + " deg C.");    	
    } // end setWeather mutator
    
    /**
     * Returns the calendar year the data was recorded.
     * @return The year the data was recorded.
     */
    public int getYear() {
        return year;
    } // end getYear

    /**
     * Returns the number of visitors.
     * @return the number of Trick or Treaters.
     */
    public int getNumMunchkins() {
        return numMunchkins;
    } // end getNumMunchkins Accessor

    /**
     * Returns the temperatures array.
     * @return The temperatures in degrees Centigrade.
     */
    public int[] getTemperatures() {
        return temperatures.clone();
    } // end getTemperature Accessor

    /**
     * Returns the weather condition.
     * @return The weather condition as a String.
     */
    public String getWeatherCondition() {
        return weatherCondition;
    } // end getWeatherCondition Accessor

    /**
     * A String representation of the current object.
     * @return A String representation of the contents of the object containing the values of
     * all the attributes.
     */
    // Overrides (replaces) the toString method of the Object class.
    @Override
    public String toString() {
        String s = "In " + year + " there were " + numMunchkins + " kids. Temperatures each hour were: ";
        for(int i = 0; i < temperatures.length - 1; i++)
            s += temperatures[i] + ", ";
        s += "and " + temperatures[temperatures.length - 1];
        s += " deg C., and the weather was ";
        s += weatherCondition + ".";
        return s;
    } // end toString

    /**
     * Tests two Halloween5 objects for equality.
     * @return <code>true</code> if all the attributes of both objects are exactly equal, <code>false</code>
     * otherwise.
     * @param otherObject The other Halloween5 object.
     */
    // Overrides the equals method of the Object class.
    @Override
    public boolean equals(Object otherObject) {
        if (otherObject instanceof Halloween5) {
            Halloween5 otherH = (Halloween5)otherObject;
            boolean arrayCheck = true;
            if (otherH.temperatures.length != temperatures.length)
                return false;
            for(int i = 0; i < temperatures.length && arrayCheck; i++)
                arrayCheck = temperatures[i] == otherH.temperatures[i];
            if (arrayCheck)
                return year == otherH.year &&
                    numMunchkins == otherH.numMunchkins &&
                    weatherCondition.equalsIgnoreCase(otherH.weatherCondition);
        } // end if
        return false;
    } // end equals

    /**
     * Compares Halloween5 objects on the basis of the number of visitors only.
     * @param otherH The other Halloween5 object.
     * @return A negative <code>int</code> if the supplied object had more vistors, zero if they have the same
     * number and a positive number if the current object has more visitors.
     */
    public int compareTo(Halloween5 otherH) {
        return numMunchkins - otherH.numMunchkins;
    } // end compareTo
    
    /**
     * Returns a copy of the of the current Halloween5 object.
     * @return A copy of the current object.
     */
    // Overrides the clone method in the Object class.
    @Override
    public Halloween5 clone() {
        Halloween5 hwCopy = null;
        try {
            hwCopy = new Halloween5(year, numMunchkins, temperatures, weatherCondition);
        } catch (IllegalHalloweenException e) {
            // Should never get here!
            return null;
        } // end try/catch
        return hwCopy;
    } // end clone
}

继承(Inheritance)

class Person {
    private String name;
    private int age;

    public String getName() {...}
    public void setName(String name) {...}
    public int getAge() {...}
    public void setAge(int age) {...}
}

class Student extends Person {
    // 不要重复name和age字段/方法,
    // 只需要定义新增score字段/方法:
    private int score;

    public int getScore() {  }
    public void setScore(int score) {  }
}

多态(Polymorphism)

接口(Interface)

内部类(Inner Class)

public interface Father {

}

public interface Mother {

}

public class Son implements Father, Mother {

}

public class Daughter implements Father{

    class Mother_ implements Mother{
        
    }
}

常用API