在Java中自定义一个类,要求两个以上的构造方法,3个以上的成员方法,运用程序完成对3个成员方法的调用
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励20(财富值+成长值)
展开全部
package com;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Student {
private int age;
private String name;
//空参构造
public Student() {
super();
}
//一个参数
public Student(int age) {
super();
this.age = age;
}
//全参构造
public Student(int age, String name) {
super();
this.age = age;
this.name = name;
}
//无返回单参方法
public void outName(String name){
System.out.println(name);
}
//静态方法
public static Date getDate(){
return new Date();
}
//静态方法
public static String outDate(String pattern){
SimpleDateFormat format = new SimpleDateFormat(pattern);
return format.format(new Date());
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Student [age=").append(age).append(", name=").append(name).append("]");
return builder.toString();
}
public static void main(String[] args) {
Student s1 = new Student();
System.out.println(s1);
Student s2 = new Student(8);
System.out.println(s2);
Student s3 = new Student(4, "jack");
System.out.println(s3);
//测试方法,同一个类中静态方法直接调用
s1.outName("sadasd");
System.out.println(getDate());
System.out.println(outDate("yyyy-MM-dd"));
}
}
import java.text.SimpleDateFormat;
import java.util.Date;
public class Student {
private int age;
private String name;
//空参构造
public Student() {
super();
}
//一个参数
public Student(int age) {
super();
this.age = age;
}
//全参构造
public Student(int age, String name) {
super();
this.age = age;
this.name = name;
}
//无返回单参方法
public void outName(String name){
System.out.println(name);
}
//静态方法
public static Date getDate(){
return new Date();
}
//静态方法
public static String outDate(String pattern){
SimpleDateFormat format = new SimpleDateFormat(pattern);
return format.format(new Date());
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Student [age=").append(age).append(", name=").append(name).append("]");
return builder.toString();
}
public static void main(String[] args) {
Student s1 = new Student();
System.out.println(s1);
Student s2 = new Student(8);
System.out.println(s2);
Student s3 = new Student(4, "jack");
System.out.println(s3);
//测试方法,同一个类中静态方法直接调用
s1.outName("sadasd");
System.out.println(getDate());
System.out.println(outDate("yyyy-MM-dd"));
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询