hp>100 && damage<50
package charactor;
public class Hero implements Comparable<Hero>{
public String name;
public float hp;
public int damage;
public Hero(){
}
public Hero(String name) {
this.name =name;
}
//初始化name,hp,damage的构造方法
public Hero(String name,float hp, int damage) {
this.name =name;
this.hp = hp;
this.damage = damage;
}
@Override
public int compareTo(Hero anotherHero) {
if(damage<anotherHero.damage)
return 1;
else
return -1;
}
@Override
public String toString() {
return "Hero [name=" + name + ", hp=" + hp + ", damage=" + damage + "]\r\n";
}
}
package lambda;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import charactor.Hero;
public class TestLambda {
public static void main(String[] args) {
Random r = new Random();
List<Hero> heros = new ArrayList<Hero>();
for (int i = 0; i < 10; i++) {
heros.add(new Hero("hero " + i, r.nextInt(1000), r.nextInt(100)));
}
System.out.println("初始化后的集合:");
System.out.println(heros);
System.out.println("筛选出 hp>100 && damange<50的英雄");
filter(heros);
}
private static void filter(List<Hero> heros) {
for (Hero hero : heros) {
if(hero.hp>100 && hero.damage<50)
System.out.print(hero);
}
}
}
public class ValueDemo
{
public static void main(String[] args)
{
int x = 10;
method(x);
System.out.println("x = " + x);
}
public static void method(int mx)
{
mx = 20;
}
}
/*结果:
x = 10
*/
public class RefDemo1
{
public static void main(String[] args)
{
Duck d = new Duck();
method(d);
System.out.println("Duck age =" + d.age);
}
public static void method(Duck duck)
{
duck.age = 5;
}
}
class Duck
{
int age = 2;//省略封装
}
/*结果:
Duck age =5
*/
/**
String字符串本身就是一个对象,跟引用传递不同,引用传递给了函数一个地址,地址指向一个值,String本
身是一个对象,就是String也是一个“类”,当给外面name小飞时,进入method,又给了sname一个小备,此时给
小备的时新给小备创建了一个地址。若name = "小飞"的地址时0x0001,函数(方法)里的name就不等于0x0001
了
*/
public class RefDemo2
{
public static void main(String[] args)
{
String name = "小飞";
method(name);
System.out.println("name = " + name);
}
public static void method(String sname)
{
sname = "小备";
}
}
/*结果:
name = 小飞
*/
public class RefDemo3
{
public static void main(String[] args)
{
Person p = new Person();
method(p);
System.out.println("Person name = " + p.name);
}
public static void method(Person per)
{
per.name = "备备";
}
}
class Person
{
String name = "飞飞"; //省略封装
}
/*结果:
Person name = 备备
*/
package socket;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class TestSocket {
public static void main(String[] args) throws UnknownHostException {
InetAddress host = InetAddress.getLocalHost();
String ip =host.getHostAddress();
System.out.println("本机ip地址:" + ip);
}
}