编写java代码声明
java代码,如何声明数组,分配内存给数组,并给数组指定初始值
java 里没有c中的malloc,只有new关键字会分配内存.
primitive types(int, float, double, char, boolean, byte)
分步:
int[] array // 此时jvm未分配内存
一步:
object types (Object)
Object[] objs; // 此时jvm未分配内存
objs[0] = new Object(); // 此时分配内存
objs[1] = new Object(); // 此时分配内存
Object[] objs = {new Object(), new Object()};
JAVA父类及子类的声明编程题(编写的代码行后面要配有必要的注释)
程序如下供你参考.说明:
①0 你题目中只提供了两门成绩,英语和计算机,但是下面说三门.
如果是的话,自己添加上去吧.很好修改的.
下面共四个java类文件.
/***Student.java **/
public class Student {
protected int studentID;
protected String name;
protected int englishScore;
protected int computerScore;
protected int totalScore;
public Student(int studentID, String name) {
super();
this.studentID = studentID;
this.name = name;
}
public Student(int studentID, String name, int englishScore,
int computerScore) {
this.englishScore = englishScore;
this.computerScore = computerScore;
public int getStudentID() {
return studentID;
public void setStudentID(Integer studentID) {
public String getName() {
return name;
public void setName(String name) {
public int getEnglishScore() {
return englishScore;
public void setEnglishScore(int englishScore) {
public int getComputerScore() {
return computerScore;
public void setComputerScore(int computerScore) {
public int getTotalScore() {
return totalScore;
/**totalScore 没有set 方法 */
@Override
public String toString() {
return "Student [studentID=" + studentID + ", name=" + name
+ ", englishScore=" + englishScore + ", computerScore="
+ computerScore + ", totalScore=" + totalScore + "]";
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
Student other = (Student) obj;
if (name == null) {
if (other.name != null)
} else if (!name.equals(other.name))
if (studentID != other.studentID)
/** @return 返回1 表示 大于,返回 0 表示等于, 返回-1表示小于 */
public int compare(Student other){
if(totalScore other.totalScore)
return -1; //
if(totalScore == other.totalScore)
return 0;
return 1;
private void sum(){
totalScore = englishScore+computerScore;
/**计算评测成绩 */
public double testScore(){
/***** StudentXW.java******/
public class StudentXW extends Student {
private String response;///责任
public StudentXW(int studentID, String name, String response) {
super(studentID, name);
this.response = response;
public StudentXW(int studentID, String name, int englishScore,
int computerScore, String response) {
super(studentID, name, englishScore, computerScore);
public String getResponse() {
return response;
public void setResponse(String response) {
/*****StudentBZ.java****/
public class StudentBZ extends Student {
public StudentBZ(int studentID, String name, String response) {
public StudentBZ(int studentID, String name, int englishScore,
/*****测试类 TestStudent.java****/
/** Student ,StudentXW, 及StudentBZ测试类 */
public class TestStudent {
public static void main(String[] args) {
Student s1 = new Student(1,"one");
System.out.println("学生 One 的评测成绩为:"+s1.testScore());
System.out.println("学生 xone,学习委员的评测成绩为:"+sx1.testScore());
System.out.println("学生 bone,班长的评测成绩为:"+sb1.testScore());
for(Student student:studentArray){
System.out.println("名字为:"+student.getName()+"的成绩为:"+ student.testScore());
想编写优美的java代码格式要记住这几条规则
做到这些规则的目的很简单,就是写出"优美"的Java代码来.
①.、Java注释尽可能全面
for(int i=0;ilist.size();i++){
System.out.println(i);}可以修改为:
for(int i=0,size=list.size();isize;i++){
String a="a";
String b="b";a=a+b;这种情况下jvm会产生"a","b","ab"三个对象.而且字符串拼接的性能也很低.所以呢通常需要做字符串处理的时候尽量采用StringBuffer和StringBuilder来.
以上就是天笑百科网小编为大家整理的编写java代码声明相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!