1. // Array of ints:
  2. int[] lottoNumbers;
  3. // Array of Strings:
  4. String[] clothingItems;
  5. // Array of ints:
  6. int[] lottoNumbers = {12, 29, 4, 38, 3};
  7. // Array of Strings:
  8. String[] clothingItems = {"Huipil", "Beanie", "Kimono", "Sari"};
  9. //
  10. String[] menuItems = new String[5];
  11. menuItems[0] = "Grilled Chicken Fajita";
  12. menuItems[1] = "Fried Plantains";
  13. menuItems[2] = "Black Bean Taco";
  14. menuItems[3] = "Chili Nachos";
  15. menuItems[4] = "Chorizo Burrito";
  1. String[] clothingItems = {"Huipil", "Beanie", "Kimono", "Sari"};
  2. System.out.println(clothingItems[2]); // Prints: Kimono
  1. String[] clothingItems = {"Huipil", "Beanie", "Kimono", "Sari"};
  2. // Change element value:
  3. clothingItems[1] = "Sweater";
  4. System.out.println(clothingItems[1]); // Prints: Sweater
  1. int[] lottoNumbers = {12, 29, 4, 38, 3};
  2. System.out.println(lottoNumbers.length); // Prints: 5
  1. //第一种方法
  2. int[] lottoNumbers = {12, 29, 4, 38, 3};
  3. for (int i = 0; i < lottoNumbers.length; i++) {
  4. // Output the current index value:
  5. System.out.println(lottoNumbers[i]);
  6. }
  7. /*
  8. Prints:
  9. 12
  10. 29
  11. 4
  12. 38
  13. 3
  14. */
  15. //第二种方法
  16. int[] lottoNumbers = {12, 29, 4, 38, 3};
  17. for (int num: lottoNumbers) {
  18. System.out.println(num);
  19. }
  20. /*
  21. Prints:
  22. 12
  23. 29
  24. 4
  25. 38
  26. 3
  27. */
  1. // 2D int array
  2. int[][] nums;
  3. int[][] nums = {{10, 9, 8}, {7, 6, 5}, {4, 3, 2}};
  4. //
  5. int[][] intArray = new int[2][3];
  6. intArray[0][0] = 1;
  7. intArray[0][1] = 2;
  8. intArray[0][2] = 4;
  9. intArray[1][0] = 1;
  10. intArray[1][1] = 3;
  11. intArray[1][2] = 6;
  1. int[][] nums = {{10, 9, 8}, {7, 6, 5}, {4, 3, 2}};
  2. // Within the first array, access the second element:
  3. System.out.println(nums[0][1]); // Prints: 9
  1. char[][] letters = {{'A', 'a'}, {'B', 'x'}, {'C', 'c'}};
  2. // Update the value:
  3. letters[1][1] = 'b';
  4. System.out.println(letters[1][1]); // Prints: b
  1. char[][] letters = {{'A', 'a'}, {'B', 'b'}, {'C', 'c'}};
  2. for (int i = 0; i < letters.length; i++){
  3. for (int j = 0; j < letters[0].length; j++){
  4. System.out.print(letters[i][j]);
  5. }
  6. }
  7. // Prints: AaBbCc
  8. char[][] letters = {{'A', 'a'}, {'B', 'b'}, {'C', 'c'}};
  9. for (int i = 0; i < letters[0].length; i++){
  10. for (int j = 0; j < letters.length; j++){
  11. System.out.print(letters[j][i]);
  12. }
  13. }
  14. // Prints: ABCabc

类和构造函数

access modifiers 访问修饰符

image.png构造函数:用来创建对象

  1. public class Car {
  2. public Car() {
  3. // Instructions for creating a Car instance
  4. System.out.println("I'm a constructor!");
  5. }
  6. public static void main(String[] args) {
  7. // Invoke the constructor:
  8. Car ferrari = new Car(); // Prints: I'm a constructor!
  9. }
  10. }
  1. class Cat {
  2. String noise;
  3. int numLives = 9;
  4. public String toString() {
  5. System.out.println("The cat with " + numLives + " lives says " + noise);
  6. }
  7. public Cat(String animalNoise){
  8. noise = animalNoise;
  9. }
  10. public static void main(String[] args) {
  11. Cat myCat = new Cat("mew");
  12. System.out.println(myCat); // Prints: The cat with 9 lives says mew
  13. }
  14. }
  1. class Shape {
  2. Shape() {
  3. System.out.println("I am a shape!");
  4. }
  5. }
  6. // Make Triangle a subclass of Shape:
  7. class Triangle extends Shape {
  8. Triangle() {
  9. System.out.print("I am a triangle!");
  10. }
  11. public static void main(String[] argos) {
  12. Shape sq = new Shape(); // Prints: I am a shape!
  13. Triangle tri = new Triangle();
  14. /* Prints:
  15. I am a shape!
  16. I am a triangle!
  17. */
  18. }
  19. }
  20. ------------------------------------------------------------------
  21. class Shape {
  22. int numSides;
  23. Shape(int numSides) {
  24. this.numSides = numSides;
  25. }
  26. }
  27. class Triangle extends Shape {
  28. Triangle() {
  29. // Use super() to call the Shape constructor:
  30. super(3);
  31. }
  32. public static void main(String[] args) {
  33. Shape sq = new Shape(4);
  34. Triangle tri = new Triangle();
  35. System.out.println(sq.numSides); // Prints: 4
  36. System.out.println(tri.numSides); // Prints: 3
  37. }
  38. }
  39. --------------------------------------------------------------------
  40. class Pho extends Noodle {
  41. public Pho() {
  42. super("flat");
  43. }
  44. }
  45. class Noodle {
  46. boolean isCooked = false;
  47. String shape;
  48. public Noodle(String shape) {
  49. this.shape = shape;
  50. }
  51. public static void main(String[] args) {
  52. Pho lunch = new Pho();
  53. System.out.println(lunch.shape);
  54. System.out.println(lunch.isCooked);
  55. }
  56. }

多态 继承的同时还保留自己的功能

  1. // Parent class:
  2. class Bird {
  3. public Bird() {
  4. // Instructions for creating a Bird go here:
  5. }
  6. public void move() {
  7. System.out.println("The bird flies away");
  8. }
  9. }
  10. // Child class:
  11. class Flamingo extends Bird {
  12. public Flamingo() {
  13. // Instructions for creating a Flamingo go here:
  14. }
  15. public static void main(String[] args) {
  16. Flamingo myFlamingo = new Flamingo();
  17. myFlamingo.move(); // The bird flies away
  18. }
  19. }
  1. // Parent class:
  2. class Bird {
  3. public Bird() {
  4. // Instructions for creating a Bird go here:
  5. }
  6. public void move() {
  7. System.out.println("The bird flies away");
  8. }
  9. }
  10. // Child class:
  11. class Penguin extends Bird {
  12. public Penguin() {
  13. // Instructions for creating a Penguin go here:
  14. }
  15. // Override the parent class method:
  16. @Override
  17. public void move() {
  18. System.out.println("The penguin waddles away");
  19. }
  20. public static void main(String[] args) {
  21. Penguin myPenguin = new Penguin();
  22. myPenguin.move(); // Prints: The penguin waddles away
  23. }
  24. }
  1. class Vet {
  2. public void Vet() {
  3. // Instructions for instance go here:
  4. }
  5. public void getCheckUp(Animal patient) {
  6. // Instructions for method go here:
  7. }
  8. public void main(String[] args) {
  9. // Create a Vet object
  10. Vet catDoctor = newVet();
  11. // Create an Animal object
  12. Animal myCat = new Cat();
  13. // Send Animal object as a parameter for a Vet method:
  14. catDoctor.getCheckUp(myCat);
  15. }
  16. }
  1. class BankAccount {
  2. protected double balance;
  3. public BankAccount(double balanceIn){
  4. balance = balanceIn;
  5. }
  6. public void printBalance() {
  7. System.out.println("Your account balance is $" + balance);
  8. }
  9. }
  10. class CheckingAccount extends BankAccount {
  11. public CheckingAccount(double balance) {
  12. super(balance);
  13. }
  14. @Override
  15. public void printBalance() {
  16. System.out.println("Your checking account balance is $" + balance);
  17. }
  18. public static void main(String[] args) {
  19. BankAccount myCheckings = new CheckingAccount(5000);
  20. myCheckings.printBalance();
  21. }
  22. }
  23. //print Your checking account balance is $5000.0