二、多个使用
新建3个接口类(A、B、C)
public interface A {
final int max = 50;
void decrease();
}
public interface B {
void add();
}
public interface C {
int i = 25;
void mult();
}
实现类(基本运算)
public class Exe implements A,B,C{
int x;
Exe(int t){
x = t;
System.out.println("x is :"+x);
}
public void decrease() {
for(int i = 1;i<=max;i++) {
if(i % 2!=0) {
x-=5;
}
}
System.out.println("the result is:"+x);
}
public void add() {
for(int i = 1;i<=max;i++) {
if(i % 3!=0) {
x+=6;
}
}
System.out.println("the result2 is:"+x);
}
public void mult() {
x*=i;
System.out.println("the result3 is:"+x);
}
}
测试类
public class Ex {
public static void main(String []args) {
Exe p = new Exe(100);
p.decrease();
p.add();
p.mult();
}
}
结果:
x is :100
the result is:-25
the result2 is:179
the result3 is:4475
数组与字符串
一、数组的概念
一组数据类型相同的有序数据的集合;中
特点:
1、数组是一种引用数据类型;
2、数组中,数组元素的类型一致;
3、数组的长度在程序运行期间不可改变。
注意事项:
1、数组是一种引用数据类型,即数组是一个对象,数组名就是对象名(或引用名),先定义后使用;
2、数组中的每个成员称为数组元素,统一的数组名和下标来唯一确定数组中的元素;
二、使用数组的步骤。
1、声明数组:声明数组名称和元素的数据类型
2、创建数组:为数组元素分配存储空间
3、使用数组:为数组元素操作(赋值、输出)
代码
public class A01 {//求最大值
public static void main(String[] args) {//main方法
int []a=new int []{12,13,53,35,67}; //定义一个数组;数据类型 []数组名称=new 数据类型[]{数组元素};
//数组:同一类型·的数据的集合
//Student a=new Student();
int max=a[0]; //定义一个变量,赋值,数组的第一个元素的值
//a[0],数组元素,下标,从0开始,最大下标,length-1;数组的长度(length:5)
for(int i=1;i<a.length;i++) { //a.length:数组的长度,i最大可以
if(max<a[i]) { //max=12 a[1]=13
max=a[i]; //max=67
}
}
System.out.println("最大值为:"+max);
}
}
一维数组(该代码写完有错误,该代码只提供书写方法和格式)
public class A02 {
int a; //声明变量
int b[]; //声明一个一维数组
int []c1; //写法推荐
//声明数组的格式:数据类型 []数组名;不能指定元素个数
e1 =new int[5]; //下标从0开始,到下标4结束,数组长度为5
e1[0] =19;
e1[1] =34;
e1[2] =56;
e1[3] =78;
e1[4] =123;
}
int []c2 = new int[] {};//静态初始化,用于数组元素较少的时候;
int []c3 = new int[5];//动态初始化:数据类型 []数组名称 = new 数据类型[数组长度];
}
数组元素的初始值,系统赋默认值
不同类型,系统给予不同的默认值
类型 | 默认值 |
---|---|
byte,short,int,long | 0 |
float,double | 0.0 |
char | '\u000’即空字符 |
boolean | false |
其他应引用类型数组(String) | null |