一、构造器
1、构造器和类有相同的名字
2、构造器的头文件没有返回值,甚至没有void,因为构造器不被显式调用,也没有返回值。
3、example
public class Rectangle {
private double length;
private double width;
public Rectangle(double len,double w){
length=len ;
width=w;
}
}
//This is how you name a constructor
//AccessSpecifier Classname(parameter...)
//This is how you use a constructor
//Rectangle box=new Rectangle(7.0,14.0)
注意!!!构造器里面一定不要写数据类型(length前不要加double),构造器外一定要有申明!!!
4、UML图(注意没有返回值)
5、默认构造器
(1)当我们不写构造器的时候,在类编译的时候,java会默认地提供一个构造器,也就是默认构造器,默认构造器不接受任何参数,它将所有值设置为0,null和false
(2)eg:
// We wrote no constructor for the Rectangle class.
Rectangle r = new Rectangle(); // Calls the default constructor
// Now we wrote our own constructor for the Rectangle class.
Rectangle box = new Rectangle(); // Error! Must now pass arguments
6、无参数构造器
public Rectangle()
{
length = 1.0;
width = 1.0;
}
// Now we have written our own no-arg constructor.
Rectangle r = new Rectangle(); // Calls the no-arg constructor
这个构造器会在不接受参数的前提下,将rectangle的两个参数设置为1和1
二、重载
1、java使用方法的签名来识别方法,签名包括方法的名称以及接受的参数,方法的返回值并不包含在方法的签名中
2、重载构造器
eg:
public Rectangle()
{
length = 0.0;
width = 0.0;
}
public Rectangle(double len, double w)
{
length = len;
width = w;
}
The following code shows an example of how each constructor is called
Rectangle box1 = new Rectangle();
Rectangle box2 = new Rectangle(5.0, 10.0);
Java仅在不为类编写任何构造函数时才提供默认构造函数。如果一个类具有接受参数的构造函数,但它没有无参数构造函数,你就不能用第一种方式实例化rectangle了。
当你写一个接受参数的类的构造函数时,如果你还想可以用第一种方式实例化类,也可以多写一个无参数构造函数。
3、例子
/**
The BankAccount class simulates a bank account.
*/
public class BankAccount
{
private double balance; // Account balance
/**
This constructor sets the starting balance
at 0.0.
*/
public BankAccount()
{
balance = 0.0;
}
/**
This constructor sets the starting balance
to the value passed as an argument.
@param startBalance The starting balance.
*/
public BankAccount(double startBalance)
{
balance = startBalance;
}
/**
This constructor sets the starting balance
to the value in the String argument.
@param str The starting balance, as a String. */
public BankAccount(String str)
{
balance = Double.parseDouble(str);
}
/**
The deposit method makes a deposit into
the account.
@param amount The amount to add to the
balance field.
*/
public void deposit(double amount)
{
balance += amount;
}
/**
The deposit method makes a deposit into
the account.
@param str The amount to add to the
balance field, as a String.
*/
public void deposit(String str)
{
balance += Double.parseDouble(str);
}
/**
The withdraw method withdraws an amount
from the account.
@param amount The amount to subtract from the balance field.
/
public void withdraw(double amount)
{
balance -= amount;
}
/**
The withdraw method withdraws an amount
from the account.
@param str The amount to subtract from
the balance field, as a String.
*/
public void withdraw(String str)
{
balance -= Double.parseDouble(str);
}
/**
The setBalance method sets the account balance.
@param b The value to store in the balance field.
*/
public void setBalance(double b)
{
balance = b;
}
/**
The setBalance method sets the account balance.
@param str The value, as a String, to store in
the balance field.
*/
public void setBalance(String str)
{
balance = Double.parseDouble(str);
}
/**
The getBalance method returns the
account balance.
@return The value in the balance field.
*/
public double getBalance()
{
return balance; }
}
三、包
1、关系
java API是java提供的一系列类的总称,而包则是一些相关的,有联系的类的集合。很多的包并不能直接使用,你需要先用关键词import去申明这个类在哪个包里,例如:
import java.util.Scanner;
申明了Scanner类
2、申明方法
第一种如上图所示
第二种
import java.util.*;
“ * ”表明申明了util这个包中所有的类(Scanner类,Random类等等都在这个包里)
ps:使用这种方法并不影响程序的大小,它只是告诉编译器你希望使这个包中所有的类都可用
3、java.lang Package以及其他包
(1)java.lang Package
这个类中包含了一些最常用的包,比如System,String,在每个java程序中,这个包会自动申明,所以可以直接使用这些类的方法
(2)其他包
四、一个小作业
public class demoCoin {
public static void main (String[] args){
Coin a=new Coin();
int heads=0;
int tails=0;
System.out.println(a.getSideUp());
for(int i=1;i<20;i++){
a.toss();
if(a.getSideUp().equals("heads"))
heads++;
else
tails++;
}
System.out.println(tails+" "+heads);
}
}
import java.util.Random;
public class Coin {
public String sideUp;
public Coin(){
Random a=new Random();
double b=a.nextDouble();
if(b<0.5)
sideUp="heads";
else
sideUp="tails";
}
public void toss(){
Random a=new Random();
double b=a.nextDouble();
if(b<0.5)
sideUp="heads";
else
sideUp="tails";
}
public String getSideUp() {
return sideUp;
}
}