Straw's B1og.

java学习(2)

字数统计: 196阅读时长: 1 min
2023/12/19

java学习(2)

外置类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

public class StudentTest {
public static void main(String[] args)
{
Student s=new Student("straw",20);
System.out.println(s.getAge());
System.out.println(s.getName());

}
}
class Student{
private String name;
private int age;
public Student()
{
System.out.println(name);
System.out.println(age);
}
public Student(String name,int age)
{
this.name=name;
this.age=age;
}
public void setName(String name)
{
this.name=name;
}
public int getAge()
{
return age;
}
public String getName()
{
return name;
}
}

输出

1
2
20
straw
字符串拼接
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

public class Demo {
public static void main(String[] args)
{
int[] arr={1,2,3};
String str=arrToString(arr);
System.out.println(str);
}
public static String arrToString(int[] arr)
{
if(arr==null)
{
return "";
}
if(arr.length==0)
{
return "[]";
}
String result= "[";
for (int i = 0; i < arr.length-1; i++)
{
result+=arr[i]+",";
}
result=result+arr[arr.length-1]+"]";
return result;
}
}

输出

1
[1,2,3]
CATALOG
  1. 1. java学习(2)
    1. 1.0.0.0.1. 外置类
    2. 1.0.0.0.2. 字符串拼接