Java基础学习之构造方法详解
作者:夏志121 时间:2021-06-16 16:25:53
一、构造方法概述
构造方法是一种特殊的方法
作用:创建对象Student stu = new Student();
格式:
pucli class 类名{
修饰符 类名(参数){
}
}
功能:主要是完成对象数据的初始化
示例代码:
class Student {
private String name;
private int age;
//构造方法
public Student() {
System.out.println("无参构造方法");
}
public void show() {
System.out.println(name + "," + age);
}
}
/*
测试类
*/
public class StudentDemo {
public static void main(String[] args) {
//创建对象
Student s = new Student();
s.show();
}
}
二、构造方法的注意事项
构造方法的创建
如果没有定义构造方法,系统将给一个默认的无参构造方法,如果定义了构造方法,系统将不再提供默认的构造方法
构造方法的重载
如果自定义了无参构造方法,还要使用无参构造方法,就必须再写一个无参数构造方法
推荐的使用方式
无论是否使用,都手工书写无参数构造方法
重要功能
可以使用带参构造,为成员变量进行初始化
示例代码:
/*
学生类
*/
class Student {
private String name;
private int age;
public Student() {}
public Student(String name) {
this.name = name;
}
public Student(int age) {
this.age = age;
}
public Student(String name,int age) {
this.name = name;
this.age = age;
}
public void show() {
System.out.println(name + "," + age);
}
}
/*
测试类
*/
public class StudentDemo {
public static void main(String[] args) {
//创建对象
Student s1 = new Student();
s1.show();
//public Student(String name)
Student s2 = new Student("小红");
s2.show();
//public Student(int age)
Student s3 = new Student(30);
s3.show();
//public Student(String name,int age)
Student s4 = new Student("小红",20);
s4.show();
}
}
三、标准类制作
需求:定义标准学生类,要求分别使用空参和有参构造方法创建对象,空参创建的对象通过setXxx赋值,有参创建的对象直接赋值,并通过show方法展示数据
示例代码:
class Student {
//成员变量
private String name;
private int age;
//构造方法
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
//成员方法
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public void show() {
System.out.println(name + "," + age);
}
}
/*
创建对象并为其成员变量赋值的两种方式
1:无参构造方法创建对象后使用setXxx()赋值
2:使用带参构造方法直接创建带有属性值的对象
*/
public class StudentDemo {
public static void main(String[] args) {
//无参构造方法创建对象后使用setXxx()赋值
Student s1 = new Student();
s1.setName("小红");
s1.setAge(20);
s1.show();
//使用带参构造方法直接创建带有属性值的对象
Student s2 = new Student("小红",20);
s2.show();
}
}
来源:https://blog.csdn.net/m0_61961937/article/details/126496928
标签:Java,构造方法
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring Boot如何使用Spring Security进行安全控制
2022-03-26 03:59:41
SpringBoot使用Jackson配置全局时间日期格式
2021-10-04 12:45:06
![](https://img.aspxhome.com/file/2023/0/83090_0s.png)
详解Spring缓存注解@Cacheable,@CachePut , @CacheEvict使用
2021-11-18 12:18:05
windows系统配置Java开发环境变量
2022-03-07 23:28:07
![](https://img.aspxhome.com/file/2023/2/62152_0s.jpg)
Java 多线程传值的四种方法
2022-02-03 08:16:44
![](https://img.aspxhome.com/file/2023/4/79944_0s.png)
使用flutter创建可移动的stack小部件功能
2023-06-21 12:28:25
![](https://img.aspxhome.com/file/2023/0/57150_0s.gif)
mybatis框架xml下trim中的prefix与suffix等标签的用法
2023-09-20 18:55:24
教你创建一个带诊断工具的.NET镜像
2021-09-28 03:11:11
![](https://img.aspxhome.com/file/2023/2/82152_0s.png)
Spring bean的实例化和IOC依赖注入详解
2023-11-23 23:57:15
Java实现在PPT中创建SmartArt图形的示例代码
2022-01-16 06:22:52
![](https://img.aspxhome.com/file/2023/3/62343_0s.jpg)
在Flutter中制作翻转卡片动画的完整实例代码
2023-06-23 23:31:21
Java SpringBoot整合shiro-spring-boot-starterqi项目报错解决
2022-11-07 03:27:02
![](https://img.aspxhome.com/file/2023/0/69650_0s.png)
c#使用Socket发送HTTP/HTTPS请求的实现代码
2023-10-12 07:10:00
![](https://img.aspxhome.com/file/2023/6/80046_0s.jpg)
JDK14性能管理工具之jstack使用介绍
2022-10-11 18:48:01
java实现通过绑定邮箱找回密码功能
2021-12-17 00:16:48
![](https://img.aspxhome.com/file/2023/0/67660_0s.png)
C#读取系统字体颜色与大小的方法
2021-12-29 07:54:57
springboot 如何设置端口号和添加项目名
2022-01-11 07:31:12
![](https://img.aspxhome.com/file/2023/5/78335_0s.png)
关于Spring Boot项目的 log4j2 核弹漏洞问题(一行代码配置搞定)
2022-08-26 03:04:20
![](https://img.aspxhome.com/file/2023/9/85539_0s.png)
SpringBoot整合Shiro框架,实现用户权限管理
2021-10-27 00:03:02
Spring-boot JMS 发送消息慢的解决方法
2023-02-06 07:50:54