java对外接口代码 Java接口代码
java怎么做对外接口
public static String sendPostUrl(String url, String param, String charset) {
目前创新互联建站已为成百上千的企业提供了网站建设、域名、虚拟空间、网站托管运营、企业网站设计、黄陵网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("a href=";tn=44039180_cprfenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1d9P103nvuBPj9-mWTkuH790ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EnHfYP1DdP1TsrHT1rHnsPjRYn0"
target="_blank"
class="baidu-highlight"user-agent/a", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), charset));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输出流、输入流
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
Java接口代码报错
public int compareTo(Object o){
if(this==o){
return 0;
这个地方,比较两个对象相等,是用Object.equal(bject o),不是用==
Java如何向外提供接口
public static String sendPostUrl(String url, String param, String charset) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), charset));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输出流、输入流
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
自定义接口的JAVA代码
三个错:
1.Chicken 类里的howtoeat方法改howtoEat;
2.Apple类和Orange 放到Fruit 类外面。
3。Chicken 类的构造方法需要给重量参数,因为你只定义了一个构造方法。
我调试的没问题,改后代码:
interface Edible{
public String howtoEat();
}
class Animal{
}
class Chicken extends Animal implements Edible,Comparable{
int weight;
public Chicken(int weight){
this.weight=weight;
}
public String howtoEat(){
return "fry it";
}
public int compareTo(Object o){
return weight-((Chicken)o).weight;
}
}
class Tiger extends Animal{
}
abstract class Fruit implements Edible
{}
class Apple extends Fruit {
public String howtoEat(){
return "Make apple cider";
}
class Orange extends Fruit{
public String howtoEat(){
return "Make orange juice";
}
}
}
public class Test{
public static void main(String[] args){
Object[] objects={new Tiger(),new Chicken(10),new Apple()};
for(int i=0;iobjects.length;i++){
showObject(objects[i]);
}
}
public static void showObject(Object object){
if(object instanceof Edible)
System.out.println(((Edible)object).howtoEat());
}
}
求JAVA接口代码
interface l1 { abstract void test(int i); } interface l2 { abstract void test(String s); } public class jiekou implements l1,l2 { public void test(int i) { System.out.println("接口1"); } public void test(String s) { System.out.println("接口2"); } public static void main(String args[]) { jiekou t=new jiekou(); t.test(42); t.test("Hello"); } } 下一个是内部接口 public class groupsix { public interface student_info { public void out(); } public class student implements student_info { int count; String name; public student(String n1) { name=n1; count++; } public void out() { System.out.println(this.name+" count="+this.count); } } public groupsix(String name1[]) { student s1; int i=0; while(iname1.length) { s1=new student(name1[i]); s1.out(); i++; } } public static void main(String args[]) { String arr[]={"A","B","C"}; groupsix g6; new groupsix(arr); } }
当前名称:java对外接口代码 Java接口代码
文章链接:http://hbruida.cn/article/hhpphp.html