// Array of ints:
int[] lottoNumbers;
// Array of Strings:
String[] clothingItems;
// Array of ints:
int[] lottoNumbers = {12, 29, 4, 38, 3};
// Array of Strings:
String[] clothingItems = {"Huipil", "Beanie", "Kimono", "Sari"};
//
String[] menuItems = new String[5];
menuItems[0] = "Grilled Chicken Fajita";
menuItems[1] = "Fried Plantains";
menuItems[2] = "Black Bean Taco";
menuItems[3] = "Chili Nachos";
menuItems[4] = "Chorizo Burrito";
String[] clothingItems = {"Huipil", "Beanie", "Kimono", "Sari"};
System.out.println(clothingItems[2]); // Prints: Kimono
String[] clothingItems = {"Huipil", "Beanie", "Kimono", "Sari"};
// Change element value:
clothingItems[1] = "Sweater";
System.out.println(clothingItems[1]); // Prints: Sweater
int[] lottoNumbers = {12, 29, 4, 38, 3};
System.out.println(lottoNumbers.length); // Prints: 5
//第一种方法
int[] lottoNumbers = {12, 29, 4, 38, 3};
for (int i = 0; i < lottoNumbers.length; i++) {
// Output the current index value:
System.out.println(lottoNumbers[i]);
}
/*
Prints:
12
29
4
38
3
*/
//第二种方法
int[] lottoNumbers = {12, 29, 4, 38, 3};
for (int num: lottoNumbers) {
System.out.println(num);
}
/*
Prints:
12
29
4
38
3
*/
// 2D int array
int[][] nums;
int[][] nums = {{10, 9, 8}, {7, 6, 5}, {4, 3, 2}};
//
int[][] intArray = new int[2][3];
intArray[0][0] = 1;
intArray[0][1] = 2;
intArray[0][2] = 4;
intArray[1][0] = 1;
intArray[1][1] = 3;
intArray[1][2] = 6;
int[][] nums = {{10, 9, 8}, {7, 6, 5}, {4, 3, 2}};
// Within the first array, access the second element:
System.out.println(nums[0][1]); // Prints: 9
char[][] letters = {{'A', 'a'}, {'B', 'x'}, {'C', 'c'}};
// Update the value:
letters[1][1] = 'b';
System.out.println(letters[1][1]); // Prints: b
char[][] letters = {{'A', 'a'}, {'B', 'b'}, {'C', 'c'}};
for (int i = 0; i < letters.length; i++){
for (int j = 0; j < letters[0].length; j++){
System.out.print(letters[i][j]);
}
}
// Prints: AaBbCc
char[][] letters = {{'A', 'a'}, {'B', 'b'}, {'C', 'c'}};
for (int i = 0; i < letters[0].length; i++){
for (int j = 0; j < letters.length; j++){
System.out.print(letters[j][i]);
}
}
// Prints: ABCabc
类和构造函数
access modifiers 访问修饰符
构造函数:用来创建对象
public class Car {
public Car() {
// Instructions for creating a Car instance
System.out.println("I'm a constructor!");
}
public static void main(String[] args) {
// Invoke the constructor:
Car ferrari = new Car(); // Prints: I'm a constructor!
}
}
class Cat {
String noise;
int numLives = 9;
public String toString() {
System.out.println("The cat with " + numLives + " lives says " + noise);
}
public Cat(String animalNoise){
noise = animalNoise;
}
public static void main(String[] args) {
Cat myCat = new Cat("mew");
System.out.println(myCat); // Prints: The cat with 9 lives says mew
}
}
class Shape {
Shape() {
System.out.println("I am a shape!");
}
}
// Make Triangle a subclass of Shape:
class Triangle extends Shape {
Triangle() {
System.out.print("I am a triangle!");
}
public static void main(String[] argos) {
Shape sq = new Shape(); // Prints: I am a shape!
Triangle tri = new Triangle();
/* Prints:
I am a shape!
I am a triangle!
*/
}
}
------------------------------------------------------------------
class Shape {
int numSides;
Shape(int numSides) {
this.numSides = numSides;
}
}
class Triangle extends Shape {
Triangle() {
// Use super() to call the Shape constructor:
super(3);
}
public static void main(String[] args) {
Shape sq = new Shape(4);
Triangle tri = new Triangle();
System.out.println(sq.numSides); // Prints: 4
System.out.println(tri.numSides); // Prints: 3
}
}
--------------------------------------------------------------------
class Pho extends Noodle {
public Pho() {
super("flat");
}
}
class Noodle {
boolean isCooked = false;
String shape;
public Noodle(String shape) {
this.shape = shape;
}
public static void main(String[] args) {
Pho lunch = new Pho();
System.out.println(lunch.shape);
System.out.println(lunch.isCooked);
}
}
多态 继承的同时还保留自己的功能
// Parent class:
class Bird {
public Bird() {
// Instructions for creating a Bird go here:
}
public void move() {
System.out.println("The bird flies away");
}
}
// Child class:
class Flamingo extends Bird {
public Flamingo() {
// Instructions for creating a Flamingo go here:
}
public static void main(String[] args) {
Flamingo myFlamingo = new Flamingo();
myFlamingo.move(); // The bird flies away
}
}
// Parent class:
class Bird {
public Bird() {
// Instructions for creating a Bird go here:
}
public void move() {
System.out.println("The bird flies away");
}
}
// Child class:
class Penguin extends Bird {
public Penguin() {
// Instructions for creating a Penguin go here:
}
// Override the parent class method:
@Override
public void move() {
System.out.println("The penguin waddles away");
}
public static void main(String[] args) {
Penguin myPenguin = new Penguin();
myPenguin.move(); // Prints: The penguin waddles away
}
}
class Vet {
public void Vet() {
// Instructions for instance go here:
}
public void getCheckUp(Animal patient) {
// Instructions for method go here:
}
public void main(String[] args) {
// Create a Vet object
Vet catDoctor = newVet();
// Create an Animal object
Animal myCat = new Cat();
// Send Animal object as a parameter for a Vet method:
catDoctor.getCheckUp(myCat);
}
}
class BankAccount {
protected double balance;
public BankAccount(double balanceIn){
balance = balanceIn;
}
public void printBalance() {
System.out.println("Your account balance is $" + balance);
}
}
class CheckingAccount extends BankAccount {
public CheckingAccount(double balance) {
super(balance);
}
@Override
public void printBalance() {
System.out.println("Your checking account balance is $" + balance);
}
public static void main(String[] args) {
BankAccount myCheckings = new CheckingAccount(5000);
myCheckings.printBalance();
}
}
//print Your checking account balance is $5000.0