코스웨어/10년 스마트폰BSP
[BSP]업무일지-박동수-20100811
알 수 없는 사용자
2010. 8. 11. 17:48
JAVA XML 이미지 삽입 소스 작성
RovotView 자식 클래스
package itisn.net;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
public class RobotView extends View {
private Drawable image;
public RobotView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
image = this.getResources().getDrawable(R.drawable.icon);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
image.setBounds(0, 0, 128, 128);
image.draw(canvas);
super.onDraw(canvas);
}
} |
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<itisn.net.RobotView
android:id="@+id/icon"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
</LinearLayout> |
위의 작업들 캡쳐 장면
java 필드 초기화 방법 3가지
1.블럭 초기화
2.생성자
3.명시적 초기화
getter 와 setter 메소드 정의와 사용하기
package exam04;
class Car
{
private int speed; //외부에서 접근 허용불가
int getSpeed()
{
return speed;
}
void setSpeed(int sp)
{
if(sp>=0)
{
speed = sp;
}
}
}
public class CarTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
Car car02 =new Car();
car02.setSpeed(10);
System.out.println(car02.getSpeed());
car02.setSpeed(-10);
System.out.println(car02.getSpeed());
}
}
|
메소드 오버로딩(최대값 구하기)
package exam05;
public class OverTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int a=10;
int b=5;
int c;
double d=10.2;
double e =5.1;
double f;
c=Math.max(a,b);
System.out.println(c);
f=Math.max(d, e);
System.out.println(f);
}
}
|
메소드 오버로딩
package exam05;
public class Overtest02 {
public static int add(int a,int b)
{
return a+b;
}
public static double add(double a,double b)
{
return a+b;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int a=10;
int b=5;
int c;
double d=10.2;
double e=5.5;
double f;
c=add(a,b);
System.out.println(c);
f=add(d,e);
System.out.println(f);
}
}
|
java this 레퍼런스 이해하기
package exam06;
class Car
{
private int speed;
public int getSpeed()
{
return speed;
}
public void setSpeed(int speed)
{
speed=speed;
}
public void speedUP(int speed)
{
this.speed=this.speed+speed;
}
public void speedUP()
{
speedUP(5);
}
}
public class CarTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Car car01=new Car();
car01.setSpeed(10);
System.out.println(car01.getSpeed());
car01.speedUP(5);
System.out.println(car01.getSpeed());
}
}
|
java 메모리영역, 초기화 순서
//1 2 5 6 3 4 7
public class TestMain{
//1
static int a;
//2
static
{
a = 1;
System.out.println("staticblock:"+a);
}
//3
{
a = 4;
System.out.println("block:"+a);
}
//4
TestMain()
{
a = 2;
System.out.println("constructor:"+a);
}
public static void main(String[] args) {
a = 3;
//5
System.out.println("main1:"+a);
//6
TestMain tt = new TestMain();
//7
System.out.println("main2:"+TestMain.a);
}
} |
//1 3 6 7 2 5 4 8
public class TestMain{
//1
static int a;
//2
public int b;
//3
static
{
a = 1;
System.out.println("staticblock, a:"+a);
b=3;//오류
}
//4
TestMain()
{
a = 2;
b = 3;
System.out.println("constructor, a:"+a);
System.out.println("constructor, b:"+b);
}
//5
{
a = 4;
System.out.println("instanceblock, a:"+a);
b = 1;
System.out.println("instanceblock, b:"+b);
}
public static void main(String[] args)
{
a = 3;
//6
System.out.println("main1, a:"+a);
//7
TestMain tt = new TestMain();
//8
System.out.println("main2, a:"+TestMain.a);
System.out.println("main2, b:"+tt.b);
}
}
|
1. static 블럭
2. main 순차적으로
3. new ->인스턴스블럭
4. 생성자
이순으로 메모리를 생성
UML 다이어그램보고 클래스 설계하기.
Product |
-name : String
-price : int |
+Product(name : String, price : int)
+Product(name : String)
+Product(price : int)
+Product()
+getName() : String
+getPrice() : int
+setName(name : String)
+setPrice(price : int) |
public class Product {
private String name;
private int price;
//블럭초기화 또는 생성자로 초기화 가능
{
name = "물";
price = 800;
}
public Product() {
name = "물";
price = 800;
}
public Product(String name, int price) {
this.name = name;
this.price = price;
}
public Product(String name) {
this.name = name;
price = 800;
//또는 this(name, 800)
}
public Product(int price) {
name = "물";
this.price = price;
//또는 this("물", price)
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public static void main(String[] args) {
Product prdt1 = new Product("웰치스",700);
System.out.println(prdt1.name + "," + prdt1.price);
Product prdt2 = new Product("커피");
System.out.println(prdt2.name + "," + prdt2.price);
Product prdt3 = new Product(500);
System.out.println(prdt3.name + "," + prdt3.price);
Product prdt4 = new Product();
System.out.println(prdt4.name + "," + prdt4.price);
}
} |