构造方法
什么是构造方法?
- 构造方法是一种特殊的方法,在创建对象时,被JVM自动调用的方法称为:构造方法。
- 作用: 创建对象 Student s = new Student() 也叫对象的实例化.
- 格式: 快捷键 alt + insert 快速生成.
- 功能: 完成初始化对象的数据.
构造方法的注意事项【理解】
- 构造方法的创建: 如果没有定义构造方法,系统将给出一个默认的无参数构造方法 如果定义了构造方法,系统将不再提供默认的构造
方法. - 重要功能!: 可以使用带参构造,为成员变量进行初始化
public class Student {
private String name;
private int age;
//构造
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
//getter setter
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
// toString方法
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
//equals方法
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
if (age != student.age) return false;
return name != null ? name.equals(student.name) : student.name == null;
}
}
/**
* 测试类
*/
public class TestDemo {
public static void main(String[] args) {
//创建对象
Student s1 = new Student();
System.out.println(s1); //Student{name='null', age=0}
Student s2 = new Student("科比", 41);
System.out.println(s2); //Student{name='科比', age=41}
Student s3 = new Student();
s3.setName("詹姆斯");
s3.setAge(36);
System.out.println(s3); //Student{name='詹姆斯', age=36}
}
}
方法
- 概述: 方法(method)是将具有独立功能的代码块组织成为一个整体,使其具有特殊功能的代码集.
- 注意:1. 方法必须先创建才可以使用,该过程成为方法定义; -------------2. 方法创建后并不是直接可以运行的,需要手动使用后,才执行,该过程成为方法调用.
- Java中方法的定义格式:
修饰符 返回值的数据类型 方法名(参数类型 参数名1, 参数类型 参数名2){ //这里可以写多个参数
//方法体;
return 具体的返回值;
}
示例:
public static void main(String[] args) {
//方法体;
}
• 修饰符: 目前记住这里是固定格式public static
• 返回值的数据类型: 用于限定返回值的数据类型的
注意:
1. 返回值的数据类型是int类型, 则说明该方法只能返回int类型的整数
2. 如果方法没有具体的返回值, 则返回值的数据类型要用void来修饰
• 方法名: 方便我们调用方法
• 参数类型: 用于限定调用方法时传入的数据的数据类型
例如: 参数类型是String类型, 说明我们调用方法时, 只能传入字符串
• 参数名: 用于接收调用方法时传入的数据的变量
• 方法体: 完成特定功能的代码
• return 返回值: 用来结束方法的, 并把返回值返回给调用者
解释: 如果方法没有明确的返回值, 则return关键字可以省略不写
形参 | 实参
-
形参:形式参数。
1.只有形式参数的参数名
2.在方法中声明的参数就是形参 -
实参:实际参数
1.有具体的数值
2.在调用方法时,传递到方法中的数据就是实参.
注意两者长度要一致.
package com.itcast.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 工具类 :
* 构造方法私有
* 成员方法静态
*/
public class DateUtils {
private DateUtils(){
}
/*
把日期转为指定格式的字符串
返回值类型:String
参数:Date date, String format
*/
public static String toStringDate(Date date, String format){
SimpleDateFormat sdf = new SimpleDateFormat(format);
String f = sdf.format(date);
return f;
}
/*
把字符串解析为指定格式的日期
返回值类型:Date
参数:String s, String format
*/
public static Date stringToDate(String s,String format) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date p = sdf.parse(s);
return p;
}
}
package com.itcast.afu01;
import com.itcast.utils.DateUtils;
import java.text.ParseException;
import java.util.Date;
/**
* 测试
*/
public class TestDemoUtils {
public static void main(String[] args) throws ParseException {
Date d1 = new Date();
String s1 = DateUtils.toStringDate(d1, "yyyy年MM月dd日 HH:mm:ss");
System.out.println(s1); //2020年11月05日 18:56:38
String s2 = DateUtils.toStringDate(d1, "yyyy年MM月dd日");
System.out.println(s2); //2020年11月05日
System.out.println("-------------------------");
String s ="2020年10月15日";
Date d = DateUtils.stringToDate(s,"yyyy年MM月dd日");
System.out.println(d); //Thu Oct 15 00:00:00 CST 2020
}
}
参数的传递
类名作为形参和返回值(应用)
-
1、类名作为方法的形参
方法的形参是类名,其实需要的是该类的对象
实际传递的是该对象的【地址值】
-
2、类名作为方法的返回值
方法的返回值是类名,其实返回的是该类的对象
实际传递的,也是该对象的【地址值】
//猫类
public class Cat {
public void eat() {
System.out.println("猫吃鱼...");
}
}
//操作类
public class CatOperator {
public void useCat(Cat c) { //Cat c = new Cat();
c.eat();
}
public Cat getCat() {
Cat c = new Cat();
return c;
}
}
//测试类
public class CatDemo {
public static void main(String[] args) {
//创建操作类对象,并调用方法
CatOperator co = new CatOperator();
Cat c = new Cat();
co.useCat(c); //猫吃鱼...
Cat c2 = co.getCat(); //new Cat()
c2.eat(); //猫吃鱼...
}
}
抽象类作为形参和返回值(理解)
- 抽象类作为形参和返回值
- 方法的形参是抽象类名,其实需要的是该抽象类的子类对象
- 方法的返回值是抽象类名,其实返回的是该抽象类的子类对象
//抽象类 动物类
public abstract class Animal {
public abstract void eat();
}
//子类
public class Cat extends Animal {
@Override
public void eat() {
System.out.println("猫吃鱼");
}
}
// 操作类
public class AnimalOperator {
public void useAnimal(Animal a) { //Animal a = new Cat();
a.eat();
}
public Animal getAnimal() {
Animal a = new Cat();
return a;
}
}
//测试类
public class AnimalDemo {
public static void main(String[] args) {
//创建操作类对象,并调用方法
AnimalOperator ao = new AnimalOperator();
Animal a = new Cat();
ao.useAnimal(a);
Animal a2 = ao.getAnimal(); //new Cat()
a2.eat();
}
}
接口作为形参和返回值
- 方法的形参是接口名,其实需要的是该接口的实现类对象
- 方法的返回值是接口名,其实返回的是该接口的实现类对象
//接口
public interface Jumpping {
void jump();
}
public class JumppingOperator {
public void useJumpping(Jumpping j) { //Jumpping j = new Cat();
j.jump();
}
public Jumpping getJumpping() {
Jumpping j = new Cat();
return j;
}
}
//实现类
public class Cat implements Jumpping {
@Override
public void jump() {
System.out.println("猫可以跳高了");
}
}
public class JumppingDemo {
public static void main(String[] args) {
//创建操作类对象,并调用方法
JumppingOperator jo = new JumppingOperator();
Jumpping j = new Cat();
jo.useJumpping(j);
Jumpping j2 = jo.getJumpping(); //new Cat()
j2.jump();
}
}