这是我的代码的一部分:
package com.admobsdk_dfp_handler;
import com.google.ads.*;
import com.google.ads.doubleclick.*;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;
import android.widget.RelativeLayout;
public class AdMobSDK_DFP_Handler extends Activity {
private DfpAdView adView;
private static final String adUnitId = "/7732/test_portal7/android_app1_test_portal7/top_banner_non_sdk_application_android_app1_test_portal7";
private Handler handler = new Handler();
private Runnable runnable = new Runnable() {
public void run() {
adView.loadAd(new AdRequest());
handler.postDelayed(this, 5000);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ad_mob_sdk__dfp__handler);
adView = new DfpAdView(this, AdSize.BANNER, adUnitId);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.mainLayout);
layout.addView(adView);
adView.loadAd(new AdRequest());
};
@Override …Run Code Online (Sandbox Code Playgroud) 在Makefile中我有:
images/schematic.pdf:images/schematic.svg
Run Code Online (Sandbox Code Playgroud)inkscape -D -z --file=$^ --export-pdf=$@ --export-latex sed -i "s:schematic:images/schematic:g" $@_tex
这条规则的作用是:
.pdfPDF文件及其相应的.pdf_tex文本文件(请参阅此答案:https://tex.stackexchange.com/a/2107/104581).pdf_tex文件,以便它不会破坏"更高"目录中的乳胶编译(即.当.pdf_tex文件在时./images)我有很多这种形式的规则,即只有schematic改变的地方.我想用一个模式规则替换schematic的%.并%在配方中使用(在sed命令中).
但是规则:
images /%.pdf:images /%.svg
Run Code Online (Sandbox Code Playgroud)inkscape -D -z --file=$^ --export-pdf=$@ --export-latex sed -i "s:%:images/%:g" $@_tex
不起作用:%在食谱中按字面解释.
我还尝试用$%替换配方中的%,但这个变量似乎是空的.
在配方中添加一行来创建一个(make)变量,该变量将保存结果notdir(removeprefix($<))(使用此问题或调用bash,因为GNU Make中没有removeprefix).
我继承的代码有许多这种形式的事务代码方法:
public void addCourseToCourses(String values)
{
try
{
conn.setAutoCommit(false);
}
catch (SQLException e)
{
Logger.getLogger(connDb.class.getName()).log(Level.SEVERE, null, e);
return;
}
try
{
stmt.executeUpdate("insert into courses values " + values);
conn.commit();
}
catch (SQLException e)
{
try
{
conn.rollback();
}
catch (SQLException ex)
{
Logger.getLogger(connDb.class.getName()).log(Level.SEVERE,null, ex);
}
}
finally
{
try
{
conn.setAutoCommit(true);
}
catch (SQLException ex)
{
Logger.getLogger(connDb.class.getName()).log(Level.SEVERE,null, ex);
}
}
}
Run Code Online (Sandbox Code Playgroud)
不同方法之间不同的变量部分是
stmt.executeUpdate("insert into courses values " + values);
Run Code Online (Sandbox Code Playgroud)
有时它是几个插入,有时它是删除.我想念使用来自C++的宏,但我确信有一种方法可以不为每个方法重复所有这些事务代码.
救命 ?
(conn和stmt是类型java.sql.Connection和java.sql.Statement的类成员)
这是一个看似简单的问题,但我无法以干净的方式进行.我有一个文件路径如下:
/这/是/的/绝对/路径/到/的/位置/的/我的/文件
我需要的是从上面给出的路径中提取/ of/my/file,因为那是我的相对路径.
我想这样做的方式如下:
String absolutePath = "/this/is/an/absolute/path/to/the/location/of/my/file";
String[] tokenizedPaths = absolutePath.split("/");
int strLength = tokenizedPaths.length;
String myRelativePathStructure = (new StringBuffer()).append(tokenizedPaths[strLength-3]).append("/").append(tokenizedPaths[strLength-2]).append("/").append(tokenizedPaths[strLength-1]).toString();
Run Code Online (Sandbox Code Playgroud)
这可能会满足我的直接需求,但是有人可以建议一种更好的方法从java中提供的路径中提取子路径吗?
谢谢
我是Camel的新手,并试图学习习语和最佳实践.我正在编写需要处理几种不同错误情况的Web服务.这是我的错误处理和路由:
onException(JsonParseException.class).inOut("direct:syntaxError").handled(true);
onException(UnrecognizedPropertyException.class).inOut("direct:syntaxError").handled(true);
// Route service through direct to allow testing.
from("servlet:///service?matchOnUriPrefix=true").inOut("direct:service");
from("direct:service")
.choice()
.when(body().isEqualTo(null))
.inOut("direct:syntaxError")
.otherwise()
.unmarshal().json(lJsonLib, AuthorizationParameters.class).inOut("bean:mybean?method=serviceMethod").marshal().json(lJsonLib);
Run Code Online (Sandbox Code Playgroud)
如您所见,我有特殊处理(基于内容的路由)来处理具有空主体的请求.有没有办法更优雅地处理这个问题?我正在写几种这种类型的服务,看起来它们可以更清洁.
在重构我的一个项目时,我用类及其常量替换所有语言ISO代码"en"和"de"字符串,并使其更加重构保存并最小化错误源.我使用locale.getLanguage()将ISO代码作为String.LocaleLocale.ENGLISHLocale.GERMAN
我使用这种方法的问题是国家和变体字段形式的Locale类的开销.我正在考虑编写自己的语言类来避免这种开销.
使用自定义课程是一种好习惯,还是已经Language错过了专门的课程?
我正在使用依赖注入来模拟类,以便对依赖于它们的其他类进行单元测试:
class Foo : IFoo
{
// production code
}
class MockFoo : IFoo
{
// mock so other classes that depend on Foo can be unit tested
}
class Bar
{
public DoSomething()
{
var barValue = 20;
// use dependency injection to get Foo instance.
DependencyInjection.Instance.Foo.ExampleMethod(barValue);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,设置我的依赖注入类变得笨拙,迷宫和复杂:
public class DependencyInjection
{
public Setup()
{
this.Foo = new Foo();
this.Bar = new Bar("example constructor string");
this.Bat = new Bat(123,234);
// for every single class in my application! …Run Code Online (Sandbox Code Playgroud) 我有以下代码,在我的班级中逐行读取文件时效果很好.
try { FileInputStream in = new FileInputStream(filename);
BufferedReader reader=new BufferedReader(new InputStreamReader(in));
String line;
while((line=reader.readLine())!=null){
// read the file
}
}
catch (Exception e) {
System.out.println(e);
}
Run Code Online (Sandbox Code Playgroud)
但是如果我尝试将命令添加为close,那么在文件读完之后,我就得到了错误:
in.close();
Error:(131, 9) java: cannot find symbol
symbol: variable in
location: class ReadFile
Run Code Online (Sandbox Code Playgroud)
我在使用后搜索了清洁对象,并在程序结束前需要关闭文件.并在Java上发现了几个帖子,但很多都非常矛盾.关键是,最后我感到非常困惑.
我错了,或者Java编程有点模糊和混乱?我的意思是,显然没有真正使用析构函数,使用finalize是非常值得怀疑的,并且使用close也被认为是不必要的.关于这些问题的一些帖子是矛盾和非决定性的.
那么,如何在这里继续?在我真的需要关闭文件的情况下,如何摆脱这个错误信息?关闭文件真的可有可无和不必要吗?如何为程序完成清理类实例?
假设我有一堂课Foo:
class Foo(object):
@staticmethod
def get_a(b, c):
if not b or not c:
raise ValueError("Invalid params!")
return b + c
def __init__(self, a=None, b=None, c=None):
if not a:
a = Foo.get_a(b, c)
self.a = a
Run Code Online (Sandbox Code Playgroud)
用户可以使用类要么a或两者b并c.如果提供,b并且c被忽略.
更好的是:提供所有三个参数时出错(确保程序员意识到正在使用哪个参数)或将其放入文档中b并且c如果提供了将被忽略的文档?
一方面,错误更明确,这是pythonic(显式优于隐式).另一方面,接受任何工作更实际(虽然实用性胜过纯度).
我有以下代码要编写。它采用一种枚举类型并返回其他枚举值。如何删除代码中太多(如果没有)条件并使其干净?
private static QuestionType parseQuestionType(QuestionTypeInfo questionTypeInfo) {
if (questionTypeInfo instanceof OpenEndedTextQuestionTypeInfo) {
return QuestionType.OPEN_ENDED;
} else if (questionTypeInfo instanceof MultiChoiceQuestionTypeInfo) {
return QuestionType.MULTI_CHOICE;
} else if (questionTypeInfo instanceof MatrixSinglePerRowQuestionTypeInfo) {
return QuestionType.MATRIX_SINGLE_PER_ROW;
} else if (questionTypeInfo instanceof OpenEndedTextQuestionTypeInfo) {
return QuestionType.OPEN_ENDED;
} else if (questionTypeInfo instanceof MatrixMultiPerRowQuestionTypeInfo) {
return QuestionType.MATRIX_MULTI_PER_ROW;
} else if (questionTypeInfo instanceof MatrixSideBySideQuestionTypeInfo) {
return QuestionType.MATRIX_SIDE_BY_SIDE;
} else if (questionTypeInfo instanceof MatrixSpreadSheetQuestionTypeInfo) {
return QuestionType.MATRIX_SPREAD_SHEET;
} else if (questionTypeInfo instanceof DataListQuestionTypeInfo) {
return QuestionType.DATA_LIST;
} else if …Run Code Online (Sandbox Code Playgroud) code-cleanup ×10
java ×6
coding-style ×2
.net ×1
android ×1
apache-camel ×1
c# ×1
class ×1
file-io ×1
filepath ×1
finalize ×1
gnu-make ×1
java-8 ×1
locale ×1
makefile ×1
oop ×1
optimization ×1
private ×1
protected ×1
python ×1
unit-testing ×1
void ×1