java 使用小技巧

Java 具有单继承,多实现的特点


String name="心痛2013WWW.sina.com.cn";
name=name.toLowerCase();
// name=name.toUpperCase();
System.out.println("变成大小写"+name);
System.out.println("result:"+name.isEmpty());
// String hure="明明爱,为何却选择要分开";
//     String temp=name.replace("心痛", "想念");
//     System.out.println(temp);
        // System.out.println(name.equals(hure));
        //比较两个内容是否相等
//System.out.println(name.charAt(0));
//下标下第零位
//System.out.println(name.endsWith(hure));
//System.out.println(name.substring(0,1).equals(hure));
//System.out.println("length:"+name.length());
//System.out.println();
//System.out.println("第2-4位的字符分别是:"+name.substring(2,4));
// System.out.println("链接结果:"+name.concat(hure));
// System.out.println("链接结果以什么结尾:"+name.concat(hure).endsWith("相守"));
// System.out.println("为的位置:"+name.concat(hure).indexOf("为"));
// String s1="thinking in java";
// String s2="欢迎学习编程思想";
// System.out.println("中文字符串比英文字符串大多少:");
// System.out.println(s2.compareTo(s1));

// String str = "TOM:89|JERRY:90|TONY:78";
// String[] s = str.split("\\|");
// String[] s1 = {"","",""};
//
// for (int i = 0; i < s1.length; i++) {
// s1[i] = s[i].replaceAll(":", "→");
// }
// for (String str1 : s1) {
// System.out.println(str1);
// }

//     String str = "TOM:89|JERRY:90|TONY:78";
//
// str = str.replaceAll(":", "→");
// System.out.println(str);
//
// String[] s = str.split("\\|");
// for (int i = 0; i < s.length; i++) {
//
// System.out.println(s[i]+"\t");
// }

// String name="Z";
// int temp=name.compareTo("b");
// System.out.println(temp);

// String name="abc";
// int temp= name.compareToIgnoreCase("abc");
// System.out.println(temp);

Date d=new Date();
//System.out.println(d);
        SimpleDateFormat sdf=new SimpleDateFormat("yyy年MM月dd日 a HH时 mm 分 ss 秒");
        
System.out.println(sdf.format(d));


//{数组格式}double []score=new double[]{0,1,2,3,4,5,6,7,8,9};  
Map<String,Object> params = this.getParametersO(request);
 String type = params.get("type")==null?"":params.get("type").toString(); 

//实体类中写构造函数后:

Teacher t=new Teacher("吴松",20);

//实体类中写方法,调用

Master m=new Master();

m.train(b);

public class SuperVehicle implements Motor,Steamer,Plane{
public void sailing()
{
   System.out.println("我是一条潜在深海的鱼");
}
public void run()
{
System.out.println("我愿驰骋在撒哈拉的沙漠之巅");
}

public void fly()
{
System.out.println("我愿意化成一颗星,点亮你的世界");
}

}

      //接口=实现类

        Motor m=new SuperVehicle();
m.run();
Plane p=new SuperVehicle();
p.fly();
Steamer s=new SuperVehicle();

s.sailing(); 


//实现中
public class SubFactory implements IFactory{

public Operation createOperation()

{

              //指向另一个类,类中写方法的实现

return new OperationSub();
}
}


public class School {
private String name;
private List<Student>list=null;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Student> getList() {
return list;
}
public void setList(List<Student> list) {
this.list = list;
}
public School(String name) {
this();
this.name = name;
}
public School() {


list=new ArrayList<Student>();
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "名字:"+this.name;
}
}

public class Student {
private String name;
private int age;
private School school;
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public School getSchool() {
return school;
}
public void setSchool(School school) {
this.school = school;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "名字:"+this.name+"年龄:"+this.age;
}

}

               School school=new School("清华大学");
Student s1=new Student("吴彤彤",20);
Student s2=new Student("张玉玲",17);
Student s3=new Student("董丹丹",18);
Student s4=new Student("赵伦芳",19);

school.getList().add(s1);
school.getList().add(s2);

Iterator<Student>iter=school.getList().iterator();
while(iter.hasNext())
{
  System.out.println(iter.next());

}

               //map 中存对象

               Map<String,Person>map=new HashMap<String,Person>();

map.put("张",new Person( "张玉玲",18));
map.put("赵", new Person("赵伦芳",19));

System.out.println(map.get("张"));


public static void main(String[] args) throws ParseException {

Scanner input=new Scanner(System.in);
System.out.println("请输入您的生日:");
String birthday=input.next();

// Date d=new SimpleDateFormat("yyyy-MM-dd").parse(birthday);
//
// System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(d));

Date d=new SimpleDateFormat("yyyy-MM-dd").parse(birthday);
System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(d));

}

try {
                Scanner input=new Scanner(System.in);
System.out.println("开始计算");
int i=0,j=0;
System.out.println("请输入第一个数字:");
String str1=input.next();
System.out.println("请输入第二个数字:");
String str2=input.next();
i=Integer.parseInt(str1);
j=Integer.parseInt(str2);
int temp=i/j;
System.out.println("结果:"+temp);
} catch (ArithmeticException e) {
e.printStackTrace();//显示堆栈信息异常
}catch(NumberFormatException e)
{
e.printStackTrace();
}catch(ArrayIndexOutOfBoundsException e)
{
e.printStackTrace();

}

//0 不能作为除数

//finally中时一定会执行的,其下面的代码也是会执行的;

try {
throw new Exception("自定义异常");
} catch (Exception e) {
e.printStackTrace();
}

                String num="123";
int temp=Integer.parseInt(num);
System.out.println(temp);

//强制转换

         if(sex.equals("男")|| sex.equals("女"))
   {
   this.sex=sex;
   }else
   {
   throw new  Exception("性别错误");

   }

//自定义异常要继承exception

public class CarWrongException extends Exception{

public CarWrongException(String message)
{
super(message);
}

}

           //三角形面积公式:

               p = (a + b + c) / 2;

double s = Math.sqrt(p * (p - a) * (p - b) * (p - c));

//二维数组
public static void main(String[] args) {
int[][]arr=new int[][]{
{45,31,23,12,14,53},
{41,54,54,47,54,13},
{41,53,41,32,12,3},
{4,53,43,21,25,6},
{43,54,13,13,24,65}
};
     
        for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j]+"\t");
}
System.out.println();
}
     

    }


//冒泡排序
public static void main(String[] args) {
int[]arr=new int[]{45,34,13,21,34,63};
for (int i = 0; i < arr.length-1; i++) {
for (int j = 0; j < arr.length-i-1; j++) {
if(arr[j]<arr[j+1]){
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}

}

        //随机数

System.out.println((int)(Math.random()*100));

for (int i = 0; i <10; i++) {
System.out.println((int)(Math.random()*100%(21-10)+10));
}

猜你喜欢

转载自blog.csdn.net/qq_35781178/article/details/80607552