20200722 千锤百炼软工人第十七天

今天再写一个类

是一个学生类

但是有一个错误没有解决

希望有大佬抬我一手

这是Student类的实现

package a20200722;
public class Student {
 private char[] no;
 private char[] name;
 private int age;
 public static int sum = 0;
 Student(char[] a,char[] b,int c){
  int i,j;
  this.age=c;
  for(i=0;i<a.length;i++)
  {
   this.no[i]=a[i];
  }
  no[i+1]='\0';
  for(j=0;j<b.length;j++)
  {
   this.name[j]=b[j];
  }
  name[j+1]='\0';
  sum++;
  System.out.print("Constructor run");
  System.out.print("NumTotal:"+sum);
 }
 
 Student(Student a){
  int i,j;
  for(i=0;i<a.no.length;i++)
  {
   this.no[i]=a.no[i];
  }
  no[i+1]='\0';
  for(j=0;j<a.name.length;j++)
  {
   this.name[j]=a.name[j];
  }
  name[j+1]='\0';
  sum++;
  System.out.print("CopyConstructor run");
 }
 Student(){
  this.no=null;
  this.name=null;
  age=0;
  System.out.print("Constructor run");
 }
 public void finalize() {
  System.out.print("Destructor run");
  System.out.print("NumTotal:"+sum);
 }
 public void setNo(char a[]) {
  int i;
  for(i=0;i<a.length;i++)
  {
   this.no[i]=a[i];
  }
  this.no[i+1]='\0';
 }
 public void setNo() {
  this.no=null;
 }
 public void setName(char a[]) {
  int i;
  for(i=0;i<a.length;i++)
  {
   this.name[i]=a[i];
  }
  this.name[i+1]='\0';
 }
 public void setName() {
  this.name=null;
 }
 public char[] getNo() {
  return this.no;
 }
 public char[] getName() {
  return this.name;
 }
 public void setAge(int a) {
  this.age=a;
 }
 public void setAge() {
  this.age=0;
 }
 public int getAge() {
  return this.age;
 }
 public void show() {
  System.out.print("No:");
  for(int i=0;i<this.no.length;i++) {
   System.out.print(this.no[i]);
  }
  System.out.print(",Name:");
  for(int j=0;j<this.name.length;j++) {
   System.out.print(this.name[j]);
  }
  System.out.print(",Age:"+this.age);
 }
}
这是主函数的实现
package a20200722;
import a20200722.Student;
public class Studentmain {
 public static void main(String[] args) { 
  char[] s1 = {'2','0','1','9','0','3','2','7'};
  char[] s2 = {'d','o','u','b','l','e','b','e','s','t'};
  Student stu1=new Student(s1,s2,0);
  stu1.setAge(21);
  stu1.show();
  Student stu2=stu1;
  stu2.setNo(s1);
  stu2.setName(s2);
  stu2.show();
  stu1=null;
  stu2=null;
  System.gc();
 }
}
posted @   黄某人233  阅读(91)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」
点击右上角即可分享
微信分享提示