目录
7.1、对象数组
案例1:
7.2、 构造方法
7.2.1、无参构造方法
7.2.2、 有参构造方法
7.3、方法重载
7.1、对象数组
对象数组的数据类型就是具体的类名,对象数组存储的就是这个类的对象,每个数组元素就是一个对象,当根据下标找到某个元素时,可以按照对象的使用方法来使用该元素
案例1:
- 定义代码
package com.bdqn.demo01;
public class Student {//仅仅用来创建对象
//定义属性
public String name;
public double score;
}
package com.bdqn.demo01;
public class StudentMethod {
//定义方法实现上述需求
public void add(Student[] stus){ //
//遍历传递过来的Student数组,取出里面每一个Student对象,然后对该对象的成绩进行判断,低于60分,提高2分
for(int i =0;i
- 调用代码
package com.bdqn.demo01;
public class StudentMethodText{
public static void main(String[] args) {
//准备一个学生对象数组
Student[] stus = new Student[3];
//创建3个学生对象
Student stu1 = new Student();
stu1.name = "zhangsan";
stu1.score = 59;
Student stu2 = new Student();
stu2.name = "lisi";
stu2.score = 49;
Student stu3 = new Student();
stu3.name = "wangwu";
stu3.score = 70;
//数组赋值
stus[0] = stu1;
stus[1] = stu2;
stus[2] = stu3;
System.out.println("调用方法之前的学生数组:");
for (int i = 0; i < stus.length; i++) {
System.out.println(stus[i].score);
}
//调用StudentMethod类中的add()方法
StudentMethod stum = new StudentMethod();
stum.add(stus);
System.out.println("调用方法之后的学生数组:");
for (int i = 0; i < stus.length; i++) {
System.out.println(stus[i].score);
}
}
}
案例2:
//定义一个方法,实现所有品牌的汽车价格都上调1000元
- 定义代码
package com.bdqn.demo02;
public class Car {
//定义属性
public String brand;
public double price;
}
package com.bdqn.demo02;
public class CarMethod {
//定义一个方法,实现所有品牌的汽车价格都上调1000元
public void add(Car[] cars){
for (int i = 0; i < cars.length; i++) {
cars[i].price+=1000;
}
}
}
- 调用代码
package com.bdqn.demo02;
public class Test {
public static void main(String[] args) {
//创建3个Car类型的对象
Car car1 = new Car();
car1.brand = "宝马";
car1.price = 200000;
Car car2 = new Car();
car2.brand = "奔驰";
car2.price = 300000;
Car car3 = new Car();
car3.brand = "奥迪";
car3.price = 400000;
//创建Car类型的数组
Car[] cs = {car1,car2,car3};
CarMethod cm = new CarMethod();
cm.add(cs);
for (int i = 0; i < cs.length; i++) {
System.out.println(cs[i].price);
}
}
}
7.2、 构造方法
Java中,当类创建一个对象时会自动调用该类的构造方法,构造方法分为
(1) 无参构造方法(默认构造方法)
(2)有参构造方法
语法结构:
访问修饰符 构造方法名(参数列表){
//初始化代码
}
- 构造方法是用来创建对象的
- 构造方法的名称与类名相同
- 构造方法没有返回值,void的也不写
- 当你在类中自定义了其它有参构造方法后,默认的无参构造方法会被覆盖,如果你还需要使用无参构造方法,就需要自己手动添加一个无参构造方法
7.2.1、无参构造方法
public class HelloWorld {
public static void main(String[] args) {
System.out.println("hello world");
}
}
将上述代码的.class文件进行反编译:
import java.io.PrintStream;
public class HelloWorld
{
//无参构造方法
public HelloWorld()
{
}
public static void main(String args[])
{
System.out.println("hello world");
}
}
7.2.2、 有参构造方法
Person定义代码:
package com.bdqn.demo03;
public class Person {
//定义属性
String name;
int age;
char gender;
double height;
double weight;
String phoneNumber;
String address;
String hobby;
String country;
String job;
String email;
//定义一个有参构造方法
public Person(String name,int age,char gender,double height,double weight,String phoneNumber,String address,String hobby,String country,String job,String email){
//this表示当前对象。一会谁调用此构造 方法创建对象,this就表示创建的这个对象
this.name = name;
this.age = age;
this.gender =gender;
this.height = height;
this.weight = weight;
this.phoneNumber=phoneNumber;
this.address = address;
this.hobby = hobby;
this.country = country;
this.job = job;
this.email = email;
}
//定义一个有参构造方法
public Person(String name,int age,char gender){
this.name = name;
this.age = age;
this.gender =gender;
}
public Person() {
}
}
测试类:
package com.bdqn.demo03;
public class PersonTeat{
public static void main(String[] args) {
// 创建Person类对象,并且给对象属性赋值
Person p1 = new Person();
p1.name = "张三";
p1.age = 30;
p1.gender = '男';
p1.height = 175.5;
p1.weight = 80;
p1.phoneNumber = "13212341234";
p1.address = "安徽合肥";
p1.hobby = "敲代码";
p1.country = "中国";
p1.job ="程序员";
p1.email = "1234@qq.com";
//可以在Person类中定义一个有参构造方法进行创建对象
Person p2 = new Person("lisi", 12, '女', 165, 50, "13245679876", "安徽安庆", "敲代码", "中国", "前端", "123@123.com");
System.out.println(p2.name);
System.out.println(p2.age);
//调用包含3个参数的构造方法创建Person对象
Person p3 = new Person("王五", 22, '男');
}
}
7.3、方法重载
/*
* 方法重载:
* 在同一个类中,多个方法的方法名相同,参数列表不同,这个现象就是方法重载,与方法的返回值和访问权限修饰符无关
* 参数列表不同体现在参数的个数不同,参数的类型不同,参数的顺序不同
//如下的4个方法构成了重载
public void getSum(int i,int j){
System.out.println("1");
}
public void getSum(double d1,double d2){
System.out.println("2");
}
public void getSum(String s ,int i){
System.out.println("3");
}
public void getSum(int i,String s){
System.out.println("4");
}
对象数组的数据类型就是具体的类名,对象数组存储的就是这个类的对象,每个数组元素就是一个对象,当根据下标找到某个元素时,可以按照对象的使用方法来使用该元素
案例1:
- 定义代码
package com.bdqn.demo01;
public class Student {//仅仅用来创建对象
//定义属性
public String name;
public double score;
}
package com.bdqn.demo01;
public class StudentMethod {
//定义方法实现上述需求
public void add(Student[] stus){ //
//遍历传递过来的Student数组,取出里面每一个Student对象,然后对该对象的成绩进行判断,低于60分,提高2分
for(int i =0;i
- 调用代码
package com.bdqn.demo01;
public class StudentMethodText{
public static void main(String[] args) {
//准备一个学生对象数组
Student[] stus = new Student[3];
//创建3个学生对象
Student stu1 = new Student();
stu1.name = "zhangsan";
stu1.score = 59;
Student stu2 = new Student();
stu2.name = "lisi";
stu2.score = 49;
Student stu3 = new Student();
stu3.name = "wangwu";
stu3.score = 70;
//数组赋值
stus[0] = stu1;
stus[1] = stu2;
stus[2] = stu3;
System.out.println("调用方法之前的学生数组:");
for (int i = 0; i < stus.length; i++) {
System.out.println(stus[i].score);
}
//调用StudentMethod类中的add()方法
StudentMethod stum = new StudentMethod();
stum.add(stus);
System.out.println("调用方法之后的学生数组:");
for (int i = 0; i < stus.length; i++) {
System.out.println(stus[i].score);
}
}
}
案例2:
//定义一个方法,实现所有品牌的汽车价格都上调1000元
- 定义代码
package com.bdqn.demo02;
public class Car {
//定义属性
public String brand;
public double price;
}
package com.bdqn.demo02;
public class CarMethod {
//定义一个方法,实现所有品牌的汽车价格都上调1000元
public void add(Car[] cars){
for (int i = 0; i < cars.length; i++) {
cars[i].price+=1000;
}
}
}
- 调用代码
package com.bdqn.demo02;
public class Test {
public static void main(String[] args) {
//创建3个Car类型的对象
Car car1 = new Car();
car1.brand = "宝马";
car1.price = 200000;
Car car2 = new Car();
car2.brand = "奔驰";
car2.price = 300000;
Car car3 = new Car();
car3.brand = "奥迪";
car3.price = 400000;
//创建Car类型的数组
Car[] cs = {car1,car2,car3};
CarMethod cm = new CarMethod();
cm.add(cs);
for (int i = 0; i < cs.length; i++) {
System.out.println(cs[i].price);
}
}
}
Java中,当类创建一个对象时会自动调用该类的构造方法,构造方法分为
(1) 无参构造方法(默认构造方法)
(2)有参构造方法
语法结构:
访问修饰符 构造方法名(参数列表){
//初始化代码
}
- 构造方法是用来创建对象的
- 构造方法的名称与类名相同
- 构造方法没有返回值,void的也不写
- 当你在类中自定义了其它有参构造方法后,默认的无参构造方法会被覆盖,如果你还需要使用无参构造方法,就需要自己手动添加一个无参构造方法
7.2.1、无参构造方法
public class HelloWorld {
public static void main(String[] args) {
System.out.println("hello world");
}
}
将上述代码的.class文件进行反编译:
import java.io.PrintStream;
public class HelloWorld
{
//无参构造方法
public HelloWorld()
{
}
public static void main(String args[])
{
System.out.println("hello world");
}
}
7.2.2、 有参构造方法
Person定义代码:
package com.bdqn.demo03;
public class Person {
//定义属性
String name;
int age;
char gender;
double height;
double weight;
String phoneNumber;
String address;
String hobby;
String country;
String job;
String email;
//定义一个有参构造方法
public Person(String name,int age,char gender,double height,double weight,String phoneNumber,String address,String hobby,String country,String job,String email){
//this表示当前对象。一会谁调用此构造 方法创建对象,this就表示创建的这个对象
this.name = name;
this.age = age;
this.gender =gender;
this.height = height;
this.weight = weight;
this.phoneNumber=phoneNumber;
this.address = address;
this.hobby = hobby;
this.country = country;
this.job = job;
this.email = email;
}
//定义一个有参构造方法
public Person(String name,int age,char gender){
this.name = name;
this.age = age;
this.gender =gender;
}
public Person() {
}
}
测试类:
package com.bdqn.demo03;
public class PersonTeat{
public static void main(String[] args) {
// 创建Person类对象,并且给对象属性赋值
Person p1 = new Person();
p1.name = "张三";
p1.age = 30;
p1.gender = '男';
p1.height = 175.5;
p1.weight = 80;
p1.phoneNumber = "13212341234";
p1.address = "安徽合肥";
p1.hobby = "敲代码";
p1.country = "中国";
p1.job ="程序员";
p1.email = "1234@qq.com";
//可以在Person类中定义一个有参构造方法进行创建对象
Person p2 = new Person("lisi", 12, '女', 165, 50, "13245679876", "安徽安庆", "敲代码", "中国", "前端", "123@123.com");
System.out.println(p2.name);
System.out.println(p2.age);
//调用包含3个参数的构造方法创建Person对象
Person p3 = new Person("王五", 22, '男');
}
}
7.3、方法重载
/*
* 方法重载:
* 在同一个类中,多个方法的方法名相同,参数列表不同,这个现象就是方法重载,与方法的返回值和访问权限修饰符无关
* 参数列表不同体现在参数的个数不同,参数的类型不同,参数的顺序不同
//如下的4个方法构成了重载
public void getSum(int i,int j){
System.out.println("1");
}
public void getSum(double d1,double d2){
System.out.println("2");
}
public void getSum(String s ,int i){
System.out.println("3");
}
public void getSum(int i,String s){
System.out.println("4");
}
public class HelloWorld {
public static void main(String[] args) {
System.out.println("hello world");
}
}
将上述代码的.class文件进行反编译:
import java.io.PrintStream;
public class HelloWorld
{
//无参构造方法
public HelloWorld()
{
}
public static void main(String args[])
{
System.out.println("hello world");
}
}
Person定义代码:
package com.bdqn.demo03; public class Person { //定义属性 String name; int age; char gender; double height; double weight; String phoneNumber; String address; String hobby; String country; String job; String email; //定义一个有参构造方法 public Person(String name,int age,char gender,double height,double weight,String phoneNumber,String address,String hobby,String country,String job,String email){ //this表示当前对象。一会谁调用此构造 方法创建对象,this就表示创建的这个对象 this.name = name; this.age = age; this.gender =gender; this.height = height; this.weight = weight; this.phoneNumber=phoneNumber; this.address = address; this.hobby = hobby; this.country = country; this.job = job; this.email = email; } //定义一个有参构造方法 public Person(String name,int age,char gender){ this.name = name; this.age = age; this.gender =gender; } public Person() { } }测试类:
package com.bdqn.demo03; public class PersonTeat{ public static void main(String[] args) { // 创建Person类对象,并且给对象属性赋值 Person p1 = new Person(); p1.name = "张三"; p1.age = 30; p1.gender = '男'; p1.height = 175.5; p1.weight = 80; p1.phoneNumber = "13212341234"; p1.address = "安徽合肥"; p1.hobby = "敲代码"; p1.country = "中国"; p1.job ="程序员"; p1.email = "1234@qq.com"; //可以在Person类中定义一个有参构造方法进行创建对象 Person p2 = new Person("lisi", 12, '女', 165, 50, "13245679876", "安徽安庆", "敲代码", "中国", "前端", "123@123.com"); System.out.println(p2.name); System.out.println(p2.age); //调用包含3个参数的构造方法创建Person对象 Person p3 = new Person("王五", 22, '男'); } }
7.3、方法重载
/*
* 方法重载:
* 在同一个类中,多个方法的方法名相同,参数列表不同,这个现象就是方法重载,与方法的返回值和访问权限修饰符无关
* 参数列表不同体现在参数的个数不同,参数的类型不同,参数的顺序不同
//如下的4个方法构成了重载
public void getSum(int i,int j){
System.out.println("1");
}
public void getSum(double d1,double d2){
System.out.println("2");
}
public void getSum(String s ,int i){
System.out.println("3");
}
public void getSum(int i,String s){
System.out.println("4");
}
/*
* 方法重载:
* 在同一个类中,多个方法的方法名相同,参数列表不同,这个现象就是方法重载,与方法的返回值和访问权限修饰符无关
* 参数列表不同体现在参数的个数不同,参数的类型不同,参数的顺序不同
//如下的4个方法构成了重载
public void getSum(int i,int j){
System.out.println("1");
}
public void getSum(double d1,double d2){
System.out.println("2");
}
public void getSum(String s ,int i){
System.out.println("3");
}
public void getSum(int i,String s){
System.out.println("4");
}



