Cirry's Blog

类之间的相互继承

2019-10-12
技术
java
最后更新:2024-06-20
1分钟
199字

StringBuffer

1
public class Demo {
2
public static void main(String[] args) {
3
StringBuffer buf = new StringBuffer();
4
for(int x = 'a'; x <'z'; x ++){
5
buf.append((char) x);
6
}
7
buf.reverse().delete(0, 5);
8
System.out.println(buf);
9
}
10
}

随机数组

1
package cn.cccc.demo;
2
import java.util.Arrays;
3
import java.util.Random;
4
class NumberFactory{
5
private static Random random = new Random();
6
7
public static int [] create(int len ){
8
int data [] = new int [len];
9
int foot = 0;
10
while(foot < data.length){
11
int num = random.nextInt(30);
12
data[foot++] = num;
13
}
14
return data;
15
}
7 collapsed lines
16
}
17
public class Demo {
18
public static void main(String[] args) {
19
int result [] = NumberFactory.create(5);
20
System.out.println(Arrays.toString(result));
21
}
22
}

抛硬币检测

1
package cn.cccc.demo;
2
import java.util.Arrays;
3
import java.util.Random;
4
class Coin {
5
private static Random random = new Random();
6
private int front;
7
private int back;
8
public void throwCoin(int count){
9
for(int x= 0; x < count ; x++){
10
int side = random.nextInt(2);
11
if(side == 0 ){
12
this.front++;
13
}else {
14
this.back++;
15
}
16 collapsed lines
16
}
17
}
18
public int getFront(){
19
return this.front;
20
}
21
public int getBack(){
22
return this.back;
23
}
24
}
25
public class Demo {
26
public static void main(String[] args) {
27
Coin coin = new Coin();
28
coin.throwCoin(1000);
29
System.out.println("正面的次数"+coin.getFront()+",背面的次数"+coin.getBack());
30
}
31
}
本文标题:类之间的相互继承
文章作者:Cirry
发布时间:2019-10-12
版权声明:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
感谢大佬送来的咖啡☕
alipayQRCode
wechatQRCode