@nextleaf
2018-08-07T19:03:51.000000Z
字数 6200
阅读 706
Java
工作日志
sssss
sssss
sss
sssssss
class StudentsTest {
String studentname;//null
char studentgender;//'\u0000'
int studentage;//0
public StudentsTest(String studentname, char studentgender, int studentage) {
this.studentname = studentname;
this.studentgender = studentgender;
this.studentage = studentage;
//System.out.println("学生类带参构造方法");
}
public StudentsTest() {System.out.println("学生类空参构造方法");}
//To introduce myself
public String selfIntroduction(){
return "我叫" +studentname+
",是一名" +studentage+
"岁的" +studentgender+
"学生";
}
}
public class StudentsArrayTest{
static int averageAge;
public static void main(String[] args){
StudentsTest [] studentsTests={
new StudentsTest("huang",'a',10),
new StudentsTest(),
new StudentsTest("wang",'男',11),
new StudentsTest("jing",'男',12),
new StudentsTest("li",'男',13),
new StudentsTest("sun",'女',14),
new StudentsTest("liu",'女',15),
new StudentsTest("Li",'b',16),
new StudentsTest("gao",'女',17),
new StudentsTest("zhao",'女',18)
};
statistics(studentsTests);
//average(studentsTests);
}
public static void statistics(StudentsTest[] students){
int numberOfMale=0;
int numberOfFemale=0;
int unknown=0;
int age=0;
for (StudentsTest student:students){
age+=student.studentage;
//if (student.studentgender=='\u0000'){}
if ('男'==student.studentgender){
numberOfMale++;
System.out.println(student.selfIntroduction());
}else if ('女'==student.studentgender){
numberOfFemale++;
System.out.println(student.selfIntroduction());
}else {
unknown++;
System.out.println("性别为空或无法判别");
}
}
averageAge=age/students.length;
System.out.println(numberOfMale+"男"+numberOfFemale+"女,还有"+unknown+"个性别未知");
//System.out.println("男女比例:1:"+((double)numberOfFemale/numberOfMale));
System.out.println("平均年龄:"+averageAge);
}
/* public static void average(StudentsTest[] students){
int age=0;
for (StudentsTest student:students){
age+=student.studentage;
}
averageAge=age/students.length;
System.out.println("平均年龄:"+averageAge);
}*/
}
运行结果
学生类空参构造方法
性别为空或无法判别
性别为空或无法判别
我叫wang,是一名11岁的男学生
我叫jing,是一名12岁的男学生
我叫li,是一名13岁的男学生
我叫sun,是一名14岁的女学生
我叫null,是一名15岁的女学生
性别为空或无法判别
我叫gao,是一名17岁的女学生
我叫zhao,是一名18岁的女学生
3男4女,还有3个性别未知
平均年龄:12
package com.nl.sx807;
/**
* Created with IntelliJ IDEA 2018.
* Description:
* User: 14472
* Date: 2018-08-07
* Time: 10:32
*/
class Waterglass {
String owner;
String content;
public Waterglass() {}
public Waterglass(String owner, String content) {
this.owner = owner;
this.content = content;
}
public static void exchange(Waterglass g1,Waterglass g2){
show(g1);show(g2);
Waterglass g=new Waterglass();
g.owner=g2.owner;
g2.owner=g1.owner;
g1.owner=g.owner;
show(g1);show(g2);
}
public static void change(Waterglass waterglass){
waterglass=new Waterglass("张无忌","可口可乐");
show(waterglass);
}
public static Waterglass loadWater(Waterglass w,String c){
w.content=c;
return w;
}
public static Waterglass pourWater(Waterglass waterglass){
waterglass.content=null;
return waterglass;
}
public static void show(Waterglass waterglass){
System.out.println(waterglass.owner+"的杯里装着"+waterglass.content);
}
}
public class WaterglassTest{
public static void main(String[] args){
Waterglass waterglass1=new Waterglass("张三丰","碧螺春");
Waterglass waterglass2=new Waterglass("凤九","普洱茶");
Waterglass waterglass3=new Waterglass("我","白开水(烧的不错)");
Waterglass.show(waterglass1);
Waterglass.show(waterglass2);
Waterglass.show(waterglass3);
System.out.println();
System.out.println("改变");
Waterglass.change(waterglass1);
Waterglass.show(waterglass1);
System.out.println();
System.out.println("交换");
Waterglass.exchange(waterglass1,waterglass2);
System.out.println();
System.out.println("装入");
Waterglass.show(Waterglass.loadWater(waterglass3,"金桔柠檬"));
System.out.println("倒掉");
Waterglass.show(Waterglass.pourWater(waterglass1));
}
}
==和equals
1)对于
==
,
如果作用于基本数据类型的变量,则直接比较其存储的 “值”是否相等
如果作用于引用类型的变量,则比较的是所指向的对象的地址
2)对于equals
方法,注意:equals方法不能作用于基本数据类型的变量
如果没有对equals方法进行重写,则比较的是引用类型的变量所指向的对象的地址;
诸如String、Date等类对equals方法进行了重写的话,比较的是所指向的对象的内容。
类
时,表明这个类不能被继承。final类中的所有成员方法都会被隐式地指定为final方法 方法
变量
,如果该变量是基本数据类型的变量,则其数值一旦在初始化之后便不能更改;如果是引用类型的变量,则在对其初始化之后便不能再让其指向另一个对象,但是它指向的对象的内容是可变的;成员变量必须在定义时或者构造器中进行初始化赋值,而且final变量一旦被初始化赋值之后,就不能再被赋值了统计学生对象数组中男女生人数以及平均年龄
class StudentsTest {
String studentname;//null
char studentgender;//'\u0000'
int studentage;//0
public StudentsTest(String studentname, char studentgender, int studentage) {
this.studentname = studentname;
this.studentgender = studentgender;
this.studentage = studentage;
}
public StudentsTest() {}
}
public class StudentsArrayTest{
static int averageAge;
public static void main(String[] args){
StudentsTest [] studentsTests={
new StudentsTest("huang",'a',10),
new StudentsTest(),
new StudentsTest("wang",'男',11),
new StudentsTest("jing",'男',12),
new StudentsTest("li",'男',13),
new StudentsTest("sun",'女',14),
new StudentsTest(null,'女',15),
new StudentsTest("Li",'b',16),
new StudentsTest("gao",'女',17),
new StudentsTest("zhao",'女',18)
};
statistics(studentsTests);
}
public static void statistics(StudentsTest[] students){
int numberOfMale=0;
int numberOfFemale=0;
int age=0;
for (StudentsTest student:students){
age+=student.studentage;
if ('男'==student.studentgender){
numberOfMale++;
}else if ('女'==student.studentgender){
numberOfFemale++;
}else {;}
}
averageAge=age/students.length;
System.out.println(numberOfMale+"男"+numberOfFemale+"女");
System.out.println("平均年龄:"+averageAge);
}
}
static用法-修饰方法举例
class Waterglass {
String owner;
String content;
public Waterglass() {}
public Waterglass(String owner, String content) {
this.owner = owner;
this.content = content;
}
public static void change(Waterglass waterglass){
waterglass=new Waterglass("张无忌","可口可乐");
show(waterglass);
}
public static void show(Waterglass waterglass){
System.out.println(waterglass.owner+"的杯里装着"+waterglass.content);
}
}
public class WaterglassTest{
public static void main(String[] args){
Waterglass waterglass1=new Waterglass("张三丰","碧螺春");
Waterglass.show(waterglass1);
Waterglass.change(waterglass1);
Waterglass.show(waterglass1);
}
}
Ctrl+Shift+J
将两行合并为一行,并删除不必要的空间以匹配代码样式
使用Refactor
| Copy
创建一个类,该类是所选类的副本。例如,当您需要创建一个与一些现有类有很多共同点的类,并且不可能将共享功能放在公共超类中时,这可能是有用的。
Ctrl+Shift+V
选择并插入文本中最近的剪贴板内容。