北京南站是高铁站吗,做网站的关键词怎么判断好不好,哈尔滨网站制作前景,WordPress地址栏Java面向对象基础知识笔记#xff08;四#xff09; 1. 对象数组的使用 在Java中#xff0c;我们可以创建包含对象的数组。对象数组是一种特殊类型的数组#xff0c;其中每个元素都是一个对象的引用。你可以将任何类的对象存储在对象数组中#xff0c;并通过索引来访问和操…Java面向对象基础知识笔记四 1. 对象数组的使用 在Java中我们可以创建包含对象的数组。对象数组是一种特殊类型的数组其中每个元素都是一个对象的引用。你可以将任何类的对象存储在对象数组中并通过索引来访问和操作这些对象。 以下是对象数组的基本用法
// 创建对象数组
ClassName[] arrayName new ClassName[size];// 实例化对象并存储到数组中
arrayName[index] new ClassName();// 访问对象数组中的元素
ClassName obj arrayName[index];// 修改对象数组中的元素
arrayName[index].propertyName value;例如我们可以创建一个Person类的对象数组
class Person {String name;int age;
}public class Main {public static void main(String[] args) {// 创建对象数组Person[] people new Person[3];// 实例化对象并存储到数组中people[0] new Person();people[0].name Alice;people[0].age 25;people[1] new Person();people[1].name Bob;people[1].age 30;people[2] new Person();people[2].name Charlie;people[2].age 35;// 访问对象数组中的元素System.out.println(Name: people[0].name , Age: people[0].age);System.out.println(Name: people[1].name , Age: people[1].age);System.out.println(Name: people[2].name , Age: people[2].age);}
}2. 使用对象作为方法的参数 在Java中我们可以将对象作为方法的参数传递。这对于需要传递多个相关数据的情况非常有用。通过将对象作为参数传递给方法我们可以方便地访问和操作该对象的属性。 以下是使用对象作为方法参数的示例
class Person {String name;int age;
}public class Main {public static void printPersonInfo(Person person) {System.out.println(Name: person.name , Age: person.age);}public static void main(String[] args) {Person person new Person();person.name Alice;person.age 25;// 调用方法并传递对象作为参数printPersonInfo(person);}
}3. 向上转型和向下转型
1. 向上转型Upcasting 向上转型是指将子类类型的引用赋值给父类类型的引用。它是一种隐式的类型转换在继承关系存在的情况下可以自动进行。 调用实例方法是要看是哪个对象调用的就执行哪个方法调用 static 方法时调用的是父类方法静态方法属于类实例方法看等号右边静态方法看等号左边。
例如如果有一个Cat类继承自Animal类那么可以将Cat对象向上转型为Animal类型
Cat cat new Cat();
Animal animal cat; // 向上转型这里cat对象被视为Animal对象因为Cat继承自Animal可以访问和操作Animal类的属性和方法。
2. 向下转型Downcasting 向下转型是指将父类类型的引用赋值给子类类型的引用。它是一种显式的类型转换需要使用强制类型转换符()来进行。 在进行向下转型之前应先使用instanceof运算符进行类型检查以确保转型是安全的。
使用 instanceof 时对象的类型必须和 instanceof 后面的参数所指定的类有继承关系否则会出现编译错误。在 java16 的增强之后对于 instanceof 的判断以及类型转换可以合二为一如下
if (pet instanceof Dog dog) {dog.catchingFlyDisc();
}
if (pet instanceof Brid bird) {brid.fly();
}以下是一个向下转型的示例
Animal animal new Cat(); // 向上转型
Cat cat;if (animal instanceof Cat) { // 类型检查cat (Cat) animal; // 向下转型// 访问和操作Cat类特有的属性和方法
}在这个示例中我们首先将Cat对象向上转型为Animal类型然后使用instanceof检查animal是否为Cat类型。如果是就可以进行向下转型并访问和操作Cat类特有的属性和方法。
多态 多态是面向对象编程的重要概念之一。它允许我们使用父类类型的引用来引用子类类型的对象从而实现不同对象的统一处理。方法重写是实现多态的基础
多态有两种形式编译时多态静态多态和运行时多态动态多态。
编译时多态重载在编译阶段根据方法的参数类型和数量来决定调用哪个方法。多个同名方法通过参数列表来区分。例如重载的print()方法可以接受不同类型的参数并根据参数类型的不同执行不同的代码逻辑。
class Printer {public void print(String str) {System.out.println(Printing string: str);}public void print(int num) {System.out.println(Printing number: num);}
}public class Main {public static void main(String[] args) {Printer printer new Printer();printer.print(Hello);printer.print(123);}
}运行时多态重写在程序运行时根据对象的实际类型来决定调用哪个方法。子类可以重写覆盖父类的方法并改变其行为。通过使用父类类型的引用来引用子类类型的对象可以实现多态。
class Animal {public void makeSound() {System.out.println(Animal is making a sound);}
}class Cat extends Animal {Overridepublic void makeSound() {System.out.println(Cat is meowing);}
}class Dog extends Animal {Overridepublic void makeSound() {System.out.println(Dog is barking);}
}public class Main {public static void main(String[] args) {Animal animal1 new Cat(); // 向上转型Animal animal2 new Dog(); // 向上转型animal1.makeSound(); // 调用Cat类的makeSound方法animal2.makeSound(); // 调用Dog类的makeSound方法}
}在上面的示例中我们创建了Animal类的两个子类Cat和Dog。通过向上转型将它们分别赋值给Animal类型的引用animal1和animal2。然后通过调用makeSound()方法可以实现多态具体执行哪个子类的方法由实际对象决定。
总结一下多态允许我们根据上下文和对象的实际类型来选择合适的方法提高代码灵活性和可复用性。根据用到的不同对象类型响应不同的操作。