最近两天主要学习了对象的方法的使用。
我编写了一个C皇的代码作为实验。
一个名为C皇的类
public class C皇 {
void Q()
{
System.out.println("大傻四方");
}
void W()
{
System.out.println("智残打击");
}
void E()
{
System.out.println("无情铁手");
}
void R()
{
System.out.println("诺克萨斯断头台");
}
void print(String 大司马){
if(大司马==金牌讲师)
{
System.out.println("回首掏,哟,鬼刀一开");
}
}
String name;
String a,b,c,金牌讲师;
C皇()
{
this.name="C皇";
this.a="脚踏孩儿鞋";
this.b="身穿父亲背心";
this.c="披着母亲斗篷";
this.金牌讲师="大司马";
}
}
测试类:
public class text1 {
public static void main(String []args)
{
C皇 text=new C皇();
System.out.println(text.name+text.a+text.b+text.c);
text.Q();
text.W();
text.E();
text.R();
String 大司马="大司马";
String 余小C="余小C";
text.print(大司马);
}
}
运行结果:
C皇脚踏孩儿鞋身穿父亲背心披着母亲斗篷
大傻四方
智残打击
无情铁手
诺克萨斯断头台
回首掏,哟,鬼刀一开
这里面我设置了几个void输出函数,现在开始实验方法的值传递。
求2~50的质数的代码。
值传递类
public class 值传递 {
boolean f(int n) {
for(int i=2;i<n;i++)
{
if(n%i==0)
{
return false;
}
}
return true;
}
}
测试代码
public class text2 {
public static void main(String [] args){
值传递 text=new 值传递();
int n=50;
for(int i=2;i<n;i++)
{
if(text.f(i))
System.out.println(i);
}
}
}
输出结果
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
也就是说Java是一种高模块化的编程语言,他习惯将每一个不同的用处作为一个类或者对象来表示。
Java语言的冒泡排序
sort类
public class sort {
public void f(int[] a,int n)
{
int t;
for(int i=0;i<n;i++)
{
for(int j=0;j<n-1-i;j++)
{
if(a[j]<a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
return;
}
}
测试类
public class text3 {
public static void main(String[] args)
{
int[] a=new int[10];
int n=10;
for(int i=0;i<n;i++)
{
a[i]=((i*201)/23+i*i+4)%57;
}
for(int i=0;i<n;i++)
{
System.out.print(a[i]+" ");
}
System.out.println();
sort b=new sort();
b.f(a,n);
for(int i=0;i<n;i++)
{
System.out.print(a[i]+" ");
}
System.out.println();
}
}
这就是我两天的学习内容总结。